interface (c # 參考) interface (C# Reference)

介面會定義合約。An interface defines a contract. 任何 class struct 執行該合約的或,都必須提供介面中所定義之成員的實作為。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 必須實作名為 SampleMethod 的方法,該方法沒有參數並會傳回 voidIn 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. 實例 auto 屬性 在介面中不受支援,因為它們會以隱含方式宣告隱藏的欄位。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. 在介面宣告中,下列程式碼不會宣告自動執行的屬性,就像在或中 class 一樣 structIn 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.

當基底類型清單包含基底類別和介面時,基底類別一定會排在清單的第一個。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.0For more information, see the Interfaces section of the C# language specification and the feature specification for Default interface members - C# 8.0

另請參閱See also