The interface is defined using the interface keyword, as shown in the following example:
InterfaceIequatable <t>
{
BoolEquals (t obj );
}
An interface describes a set of functions that can belong to any class or structure. An interface can be composed of methods, properties, events, indexers, or any combination of the four member types. The interface cannot contain fields. The interface members must be public.
When a class or structure inherits an interface, it means that the class or structure provides implementation for all the Members defined by the interface. The interface itself does not provide any function that the class or structure can inherit in the form of inheriting the basic class function. However, if the base class implements the interface, the derived class inherits this implementation.
Classes and structures can inherit interfaces in a similar way of inheriting the base class or structure, but there are two exceptions:
-
The class or structure can inherit multiple interfaces.
When a class or structure inherits an interface, only the method name and signature are inherited, because the interface itself does not contain an implementation. For example:
Public Class Car: iequatable <car>
{
Public String Make { Get ;Set ;}
Public String Model { Get ; Set ;}
Public String Year { Get ; Set ;}
// Implementation of iequatable <t> Interface
Public Bool Equals (car)
{
If ( This . Make = car. Make &&
This . Model = car. Model &&
This . Year = car. Year)
{
Return True ;
}
Else
Return False ;
}
}
To implement an interface member, the corresponding member in the class must be public and non-static, and have the same name and signature as the interface member. The class attributes and indexer can define additional accessors for the attributes or indexer defined on the interface. For example, an interface can declare an attribute with a get accesser, and the class implementing this interface can declareGetThe same attribute as set accessors. However, if the attribute or indexer is explicitly implemented, the accessors must match.
Interfaces and interface members are abstract; interfaces do not provide default implementations. For more information, see abstract classes, seal classes, and class members.
Iequatable(T)The interface declares to the object user that the object can determine whether it is equal to other objects of the same type, and the interface user does not need to know the implementation method.
Interfaces can inherit from other interfaces. Class can inherit an interface multiple times through its inherited base class or interface. In this case, if the interface is declared as a part of the new class, the class can only implement this interface once. If the inherited interface is not declared as a part of the new class, its implementation will be provided by the base class that declares it. The base class can use virtual members to implement interface members. In this case, the class inheriting interfaces can modify interface behavior by overwriting virtual members. For more information about virtual members, see polymorphism.
Interface Overview
The interface has the following attributes:
An interface is similar to an abstract base class: any non-Abstract type that inherits an interface must implement all the members of the interface.
The interface cannot be instantiated directly.
Interfaces can contain events, indexers, methods, and attributes.
The interface does not contain the implementation of methods.
Classes and structures can be inherited from multiple interfaces.
The interface itself can be inherited from multiple interfaces.
Content of this section
- Explicit interface implementation (C # programming guide)
-
Explains how to create interface-specific class members.
- How to: explicitly implement interface members (C # programming guide)
-
Provides an example of how to explicitly implement interface members.
- How to: Use inheritance to explicitly implement interface members (C # programming guide)
-
Provides an example of how to explicitly implement interface members through inheritance.
Related chapters
- Interface attributes (C # programming guide)
-
Describes how to declare attributes on an interface.
- Interface indexer (C # programming guide)
-
Explains how to declare the indexer on the interface.
- How to: implement interface events (C # programming guide)
-
Demonstrate how the interface declares events.
- Class and structure (C # programming guide)
-
Describes how to use objects, classes, and structures in C.
Display Interface implementation:
If the class implements two interfaces and these two interfaces contain members with the same signature, implementing this member in the class will result in both interfaces using this member as their implementation. For example:
InterfaceIControl
{
VoidPaint ();
}
InterfaceIsurface
{
VoidPaint ();
}
ClassSampleclass: iControl, isurface
{
//Both isurface. Paint and iControl. Paint call this method.
Public VoidPaint ()
{
}
}
However, if two interface members execute different functions, this may result in incorrect implementation of one of the interfaces or incorrect implementation of both interfaces. You can explicitly implement interface Members-that is, create a class member that is called only through this interface and is specific to this interface. This is implemented by using the interface name and a period to name the class members. For example:
Public ClassSampleclass: iControl, isurface
{
VoidIControl. Paint ()
{
System. Console. writeline ("IControl. Paint");
}
VoidIsurface. Paint ()
{
System. Console. writeline ("Isurface. Paint");
}
}
Class MemberIControl. PaintOnlyIControlInterface Usage,Isurface. PaintOnlyIsurface. Both methods are separated and cannot be directly used in the class. For example:
Sampleclass OBJ =NewSampleclass ();
//OBJ. Paint ();//Compiler error.
IControl c = (iControl) OBJ;
C. Paint ();//Callicontrol. Paint on sampleclass.
Isurface S = (isurface) OBJ;
S. Paint ();//CILS isurface. Paint on sampleclass.
Explicit implementation is also used to resolve the situation where two interfaces declare different members with the same name (such as attributes and methods:
InterfaceIleft
{
IntP {Get;}
}
InterfaceIright
{
IntP ();
}
To implement both interfaces at the same time, the class must use explicit implementation for Attribute P and/or Method P to avoid compiler errors. For example:
ClassMiddle: ileft, iright
{
Public IntP (){Return 0;}
IntIleft. P {Get{Return 0;}}
}
Show implementation interface members:
This example declares an InterfaceIdimensionsAnd a classBoxThis class explicitly implements interface members.GetlengthAndGetwidth. Interface instanceDimensionsAccess these members.
Example
Interface Idimensions
{
Float Getlength ();
Float Getwidth ();
}
Class Box: idimensions
{
Float Lengthinches;
Float Widthinches;
Box ( Float Length, Float Width)
{
Lengthinches = length;
Widthinches = width;
}
// Explicit interface member implementation:
Float Idimensions. getlength ()
{
Return Lengthinches;
}
// Explicit interface member implementation:
Float Idimensions. getwidth ()
{
Return Widthinches;
}
Static Void Main ()
{
// Declare a class instance box1:
Box box1 = New Box ( 30366f , 20366f );
// Declare an interface instance dimensions:
Idimensions dimensions = (idimensions) box1;
// The following commented lines wowould produce Compilation
// Errors because they try to access an explicitly implemented
// Interface member from a class instance:
// System. Console. writeline ("Length: {0}", box1.getlength ());
// System. Console. writeline ("width: {0}", box1.getwidth ());
// Print out the dimensions of the box by calling the methods
// From an instance of the interface:
System. Console. writeline ( " Length: {0} " , Dimensions. getlength ());
System. Console. writeline ( " Width: {0} " , Dimensions. getwidth ());
}
}
/* Output:
Length: 30
Width: 20
*/
Reliable Programming
Note:MainThe method is as follows:CodeLines are commented out because they will produce compilation errors. Explicitly implemented interface members cannot be accessed from class instances:
//System. Console. writeline ("Length: {0}", box1.getlength ());
//System. Console. writeline ("width: {0}", box1.getwidth ());
Note that,MainThe following code lines in the method successfully output the size of the box, because these methods are called from the interface instance:
-
System. Console. writeline ("Length: {0}", Dimensions. getlength ());
System. Console. writeline ("Width: {0}", Dimensions. getwidth ());