interface(C#参考)interface (C# Reference)

接口定义协定。An interface defines a contract. 实现该协定的任何 classstruct 必须提供接口中定义的成员的实现。Any class or struct that implements that contract must provide an implementation of the members defined in the interface. 从 C# 8.0 开始,接口可为成员定义默认实现。Beginning with C# 8.0, an interface may define a default implementation for members. 它还可以定义 static 成员,以便提供常见功能的单个实现。It may also define static members in order to provide a single implementation for common functionality.

在以下示例中,类 ImplementationClass 必须实现一个不含参数但返回 void 的名为 SampleMethod 的方法。In the following example, class ImplementationClass must implement a method named SampleMethod that has no parameters and returns void.

有关详细信息和示例,请参阅接口For more information and examples, see Interfaces.

示例Example

interface ISampleInterface
{
    void SampleMethod();
}

class ImplementationClass : ISampleInterface
{
    // Explicit interface member implementation:
    void ISampleInterface.SampleMethod()
    {
        // Method implementation.
    }

    static void Main()
    {
        // Declare an interface instance.
        ISampleInterface obj = new ImplementationClass();

        // Call the member.
        obj.SampleMethod();
    }
}

接口可以是命名空间或类的成员。An interface can be a member of a namespace or a class. 接口声明可以包含以下成员的声明(没有任何实现的签名):An interface declaration can contain declarations (signatures without any implementation) of the following members:

上述成员声明通常不包含主体。These preceding member declarations typically do not contain a body. 从 C# 8.0 开始,接口成员可以声明主体。Beginning with C# 8.0, an interface member may declare a body. 这称为“默认实现” 。This is called a default implementation. 具有主体的成员允许接口为不提供重写实现的类和结构提供“默认”实现。Members with bodies permit the interface to provide a "default" implementation for classes and structs that don't provide an overriding implementation. 此外,从 C# 8.0 开始,接口可以包括:In addition, beginning with C# 8.0, an interface may include:

接口不能包含实例状态。Interfaces may not contain instance state. 虽然现在允许使用静态字段,但接口中不允许使用实例字段。While static fields are now permitted, instance fields are not permitted in interfaces. 接口中不支持实例自动属性,因为它们将隐式声明隐藏的字段。Instance auto-properties are not supported in interfaces, as they would implicitly declare a hidden field. 此规则对属性声明有细微影响。This rule has a subtle effect on property declarations. 在接口声明中,以下代码不会像在 classstruct 中一样声明自动实现的属性。In an interface declaration, the following code does not declare an auto-implemented property as it does in a class or struct. 相反,它会声明一个属性,该属性没有默认实现,而必须在该实现接口的任何类型中实现它:Instead, it declares a property that doesn't have a default implementation but must be implemented in any type that implements the interface:

public interface INamed
{
  public string Name {get; set;}
}

一个接口可从一个或多个基接口继承。An interface can inherit from one or more base interfaces. 当接口 重写基接口中的方法实现时,必须使用显式接口实现语法。When an interface overrides a method implemented in a base interface, it must use the explicit interface implementation syntax.

基类型列表包含基类和接口时,基类必须是列表中的第 1 项。When a base type list contains a base class and interfaces, the base class must come first in the list.

实现接口的类可以显式实现该接口的成员。A class that implements an interface can explicitly implement members of that interface. 显式实现的成员不能通过类实例访问,而只能通过接口实例访问。An explicitly implemented member cannot be accessed through a class instance, but only through an instance of the interface. 此外,只能通过接口实例访问默认接口成员。In addition, default interface members can only be accessed through an instance of the interface.

有关显式接口实现的详细信息,请参阅显式接口实现For more information about explicit interface implementation, see Explicit Interface Implementation.

示例Example

下例演示了接口实现。The following example demonstrates interface implementation. 在此示例中,接口包含属性声明,类包含实现。In this example, the interface contains the property declaration and the class contains the implementation. 实现 IPoint 的类的任何实例都具有整数属性 xyAny instance of a class that implements IPoint has integer properties x and y.

interface IPoint
{
   // Property signatures:
   int X
   {
      get;
      set;
   }

   int Y
   {
      get;
      set;
   }

   double Distance
   {
       get;
   }
}

class Point : IPoint
{
   // Constructor:
   public Point(int x, int y)
   {
      X = x;
      Y = y;
   }

   // Property implementation:
   public int X { get; set; }

   public int Y { get; set; }

   // Property implementation
   public double Distance =>
      Math.Sqrt(X * X + Y * Y);

}

class MainClass
{
   static void PrintPoint(IPoint p)
   {
      Console.WriteLine("x={0}, y={1}", p.X, p.Y);
   }

   static void Main()
   {
      IPoint p = new Point(2, 3);
      Console.Write("My Point: ");
      PrintPoint(p);
   }
}
// Output: My Point: x=2, y=3

C# 语言规范C# language specification

有关详细信息,请参阅 C# 语言规范接口部分和 默认接口成员 - C# 8.0 的功能规范For more information, see the Interfaces section of the C# language specification and the feature specification for Default interface members - C# 8.0

请参阅See also