Interface,在C#中是一個關鍵字,它包含一組抽象方法和屬性,這些方法和屬性由抽像或非抽象類別實作或使用。定義方法是介面內的屬性,預設情況下它們是公共和抽象的。
用最簡單的話來說,介面就像一個契約,主體中包含的每個成員或元件都必須遵循契約,它定義了必須做什麼。該介面不包含任何字段,並且始終透過使用關鍵字“interface”來定義。
文法:
語法以介面關鍵字開頭,後面跟著介面名稱,然後是正文。
interface <name_for_interface> { //abstract methods //abstract properties. }
如您所見,我們有 C# 中介面的標準語法,它以「interface」關鍵字開頭,然後是介面的名稱,然後是主體內的抽象方法和屬性。在 C# 中,可以在類別或結構內部實作和使用多個介面。這些介面可以將各種方法、索引器、屬性以及事件作為成員。
基本上我們已經明白介面內部沒有特定的功能,如果是這樣,那我們為什麼需要介面?
什麼時候使用介面?
public class MyClass : IMyInterface { public void Method1() { // Method implementation }public string Property1 { get; set; } public event EventHandler Event1; }
現在我們已經了解了什麼是介面及其需求。讓我們示範一個帶有介面實現的 C# 程式碼的簡單範例。
程式實作介面並列印一條簡單的語句。
代碼:
using System; namespace MyApplication { interface SampleInterface { void InterfaceMethod(); } class Int_Example : SampleInterface { public void InterfaceMethod() { Console.WriteLine("\nThis is simple example of Interface in C#."); } } class Program { static void Main(string[] args) { Int_Example myInterface = new Int_Example(); myInterface.InterfaceMethod(); Console.Read(); } } }
程式碼解釋:從使用和命名空間開始,產生一個基本介面作為 SampleInterface,其主體中有一個方法。介面內的該方法沒有任何特定的主體。然後我們有新的類別來實作我們創建的介面。使用 class 關鍵字創建,後面跟著類別名,然後使用冒號符號後面跟著介面名稱來實作介面。在我們的 Int_Example 類別中,我們有先前建立的介面方法,當時它還沒有實體,現在我們加入了簡單的列印語句,它說:「這是 C# 中介面的簡單範例。」
Then begins our mail class, namely Program, with the static void main statement. Inside our main class, we have created a new object for our Int_Example class which inherits interface. The new object is created and to the next line, our method created earlier is called up. Finally, our newly created object will call the earlier created method and the body inside that method will be executed here. With Console.Read(); the program will wait for user input before exiting.
Output:
Upon successful compilation and execution, the program must simply print the statement: “This is a simple example of Interface in C#.”
Arithmetic operations using the interface.
Code:
using System; namespace arth_interface { public interface SampleInterface { void sam_add(int a, int b); void sam_sub(int a, int b); void display(); } class interface_class : SampleInterface { int x, y; public void sam_add(int a, int b) { int m, n; m = a; n = b; x = m + n; } public void sam_sub(int a, int b) { int m, n; m = a; n = b; y = a - b; } public void display() { Console.WriteLine("Added Value is:" + x); Console.WriteLine("Subtracted value is:" + y); } } class arth_interface { static void Main(string[] args) { interface_class obj_interface_class = new interface_class(); int fnumber, snumber; Console.WriteLine("Please Enter 1st Number to perform Addition and Subtraction:"); fnumber = Convert.ToInt16(Console.ReadLine()); Console.WriteLine("Now 2nd Number to perform Addition and Subtraction:"); snumber = Convert.ToInt16(Console.ReadLine()); obj_interface_class.sam_add(fnumber, snumber); obj_interface_class.sam_sub(fnumber, snumber); obj_interface_class.display(); Console.ReadKey(); } } }
Code Interpretation: Similar to our first example, we have used and namespace statements, followed by the interface and its body with methods. We have two basic methods for addition and subtraction with void as return type, two integers inside every method, respectively. Next, we have our class which implements our interface.
We’ve declared two integers and then we have our first method to calculate addition. Here is the operation that needs to be done for addition and the same is for the subtraction. Then we have our display method, which consists of two print statements, printing addition and subtraction values of the numbers passed.
Finally, we have our class with the main method, where we initially created an object for our interface. Then the program prints “Please Enter the 1st Number to perform Addition and Subtraction:”, where the user inputs a first number and the later second number, for the purpose of calculations. With the object created earlier, the program calls the add and sub-methods from the interface and the same operations are done. At last, we have our display method, which displays our results as defined in the display method and ReadKey(); method holds up our program until any key is pressed.
Output:
Below are some of the advantages given.
We have understood what Interface in C# is. The proper syntax for an interface along with an explanation. To wrap it up, Interfaces in C# are a way to fill the emptiness of multiple inheritances in the language. Later we learned why do we actually need the interface in C# followed by the examples to demonstrate the understanding of the interfaces. The first example was to demonstrate simple use of interface while with the second example we implemented arithmetic operations, followed by Code Interpretation and output screenshot.
以上是C# 介面的詳細內容。更多資訊請關注PHP中文網其他相關文章!