What is a C# interface?
And then what? C# interface is used to define the contract of a program. The class or structure that implements the interface should be strictly consistent with the definition of the interface. With this protocol, you can set aside the limitation of programming language (in theory). C# interfaces can inherit from multiple base interfaces, while classes or structures can implement multiple interfaces. C# interfaces can contain methods, properties, events and indexers. The interface itself does not provide the implementation of the members it defines. An interface only specifies the members that the class or interface that implements it must provide. C# interface is like a template, which defines the methods that an object must implement, so that these methods can be referenced as interface instances. Interfaces cannot be instantiated. A class can implement multiple interfaces and index through these interfaces. Interface variables can only index instances of classes that implement the interface. Example: interface imyexample {stringthis [int index] {get; Settings; } event EventHandler Evenvoid Find(int value); String point {get setting; }} public delegate void EventHandler (object sender, event e); The C# interface in the above example contains an index this, an event even, a method Find and an attribute Point. C# interface can support multiple inheritance. Just like the following example, the interface "IComboBox" inherits both "ITextBox" and "IListBox". Interface icontrol {void paint (); } interface ITextBox: IControl {void SetText (string text); } interface ilistbox: icontrol {void set items (string [] items); } interface icombobox: itextbox, ilistbox {} classes and structures can instantiate the C# interface many times. Just like the following example, the "EditBox" class inherits the "Control" class and inherits both "IDataBound" and "IControl". Interface idatabound {void bind (binder b); } blic class EditBox: Control, IControl, idatabound {public void paint (); Public void Bind(Binder b) {}} In the above code, the "Paint" method comes from the "IControl" interface; The "Bind" method comes from the "IDataBound" interface and is implemented as "public" in the "EditBox" class. C# Interface Overview: The interfaces in 1 and C# are independent of the class definition. This is contrary to the C++ model, in which interfaces are actually abstract base classes. 2. Both interfaces and classes can inherit multiple interfaces. 3. Although the class can inherit the base class, the interface cannot inherit the class at all. This model avoids the problem of multiple inheritance in C++, and the implementations in different base classes in C++ may conflict. Therefore, complex mechanisms such as virtual inheritance and explicit scope are no longer needed. The simplified interface model of C# helps to speed up the development of applications. 4. The interface defines a reference type with only abstract members. What an interface in C# actually does is to have a method flag, but there is no execution code at all. This means that interfaces cannot be instantiated, only objects derived from interfaces can be instantiated. 5. Interfaces can define methods, properties and indexes. Therefore, compared with a class, the particularity of interfaces is that when defining a class, you can derive from multiple interfaces, but you can only derive from one class. The basic situation of C# interface is introduced here, hoping to help you understand the meaning and usage of C# interface.