Vous êtes sur la page 1sur 3

Interfaces (C# Programming Guide)

Interfaces are defined using the interface keyword. For example:

interface IComparable { int CompareTo(object obj); }


Interfaces describe a group of related behaviors that can belong to any class or struct. Interfaces can be made up of methods, properties, events, indexers, or any combination of those four member types. An interface can not contain fields. Interfaces members are automatically public. Classes and structs can inherit from interfaces in a manner similar to how classes can inherit a base class or struct, with two exceptions:

A class or struct can inherit more than one interface. When a class or struct inherits an interface, it inherits only the method names and
signatures, because the interface itself contains no implementations. For example:

public class Minivan : Car, IComparable { public int CompareTo(object obj) { //implementation of CompareTo return 0; //if the Minivans are equal } }
To implement an interface member, the corresponding member on the class must be public, non-static, and have the same name and signature as the interface member. Properties and indexers on a class can define extra accessors for a property or indexer defined on an interface. For example, an interface may declare a property with a get accessor, but the class implementing the interface can declare the same property with both a get and set accessor. However, if the property or indexer uses explicit implementation, the accessors must match. Interfaces and interface members are abstract; interfaces do not provide a default implementation. For more information, see Abstract and Sealed Classes and Class Members. The IComparable interface announces to the user of the object that the object can compare itself to other objects of the same type, and the user of the interface does not need to know how this is implemented. Interfaces can inherit other interfaces. It is possible for a class to inherit an interface multiple times, through base classes or interfaces it inherits. In this case, the class can only implement the interface once, if it is declared as part of the new class. If the inherited

interface is not declared as part of the new class, its implementation is provided by the base class that declared it. It is possible for a base class to implement interface members using virtual members; in that case, the class inheriting the interface can change the interface behavior by overriding the virtual members. Interfaces Overview An interface has the following properties:

An interface is similar to an abstract base class: any non-abstract type inheriting the
interface must implement all its members.

An interface cannot be instantiated directly. Interfaces can contain events, indexers, methods and properties. Interfaces contain no implementation of methods. Classes and structs can inherit from more than one interface. An interface can itself inherit from multiple interfaces.

How to: Explicitly Implement Interface Members


This example declares an interface, IDimensions, and a class, Box, which explicitly implements the interface members getLength and getWidth. The members are accessed through the interface instance dimensions.

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(30.0f, 20.0f); // Declare an interface instance dimensions: IDimensions dimensions = (IDimensions)box1;
// The following commented lines would 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 Robust Programming Notice that the following lines, in the Main method, are commented out because they
would produce compilation errors. An interface member that is explicitly implemented cannot be accessed from a class instance:

//System.Console.WriteLine("Length: {0}", box1.getlength()); //System.Console.WriteLine("Width: {0}", box1.getwidth()); Notice also that the following lines, in the Main method, successfully print out the
dimensions of the box because the methods are being called from an instance of the interface:

System.Console.WriteLine("Length: {0}", dimensions.getLength()); System.Console.WriteLine("Width: {0}", dimensions.getWidth());

Vous aimerez peut-être aussi