C# Language Reference
Interface (C# Reference)
Definition: An interface is a form of constraint that only includes member definitions and does not include member implementation content .
Purpose: The main purpose of the interface is to provide general processing services for unrelated classes. Since C# only allows single inheritance in the tree structure, that is, a class can only inherit one parent class, so the interface Is the only way to have a class with more than two base classes.
Declaration: The way to declare an interface is similar to the way to declare a class, but the keyword used is interface instead of class.
Interfaces only contain signatures for methods, properties, indexers, and events. The implementation of the method is done in the class that implements the interface, as shown in the following example: interface?ISampleInterface{//interfacemembersvoid?SampleMethod();}class?Implementation?Class:ISampleInterface{//Implementing the interface members. void?ISampleInterface.SampleMethod(){//Method implementation. Main();}static?void?Main(){//Define an instance variable obj of an interface. ISampleInterface?obj=new?ImplementationClass();//Call the member method of (obj). Console.Write(ok);Console.Read();}} The definition of interface members: similar to the definition of class members, but please note:
The use of access modifiers (public, private, protected is not allowed or internal), all interface members must be public;
Interface members cannot contain code entities;
Interface members cannot define field members;
< p>Interface members cannot be defined using the keywords static, virtual, abstract or sealed;Type definition members are prohibited.
If you want to hide members that inherit the base interface, you can use the keyword new to define them, for example: interface?IMyBaseInterface{void?DoSomething();}interface?IMyDerivedInterface: IMyBaseInterface{new?void?DoSomething ();}Remarks:
An interface can be a member of a namespace or a class, and can contain the signatures of the following members:
Method Attribute Indexer Event An interface can be retrieved from one or more A base interface is inherited.
When the base type list contains a base class and an interface, the base class must be the first item in the list.
A class that implements an interface can explicitly implement the members of the interface. Explicitly implemented members are not accessible through class instances, but only through interface instances, for example:
For more details and code examples about explicit interface implementation, see Explicit Interface Implementation (C# Programming Guide).
Example
The following example demonstrates interface implementation. In this example, the interface IPoint contains property declarations, which are responsible for setting and getting the field's value. The Point class contains property implementations.
//keyword_interface_2.cs//Interfaceimplementationusing?System;interfaceIPoint{//Propertysignatures:intx{get; Set;}inty{get; set;}}class?Point:IPoint{//Fields:private?int?_x;private? int?_y;//Constructor:public?Point(int?x,int?y){_x=x;_y=y;}//Propertyimplementation:public?int?x{get{return?_x;}set{_x =value;}}public?int?y{get{return?_y;}set{_y=value;}}}class?MainClass{static?void?PrintPoint(IPointp){Console.WriteLine(x={0}, y={1},p.x,p.y);}static?void?Main(){Pointp=newPoint(2,3);Console.Write(MyPoint:);PrintPoint(p);}Output MyPoint:x=2, y=3