介面 (C# 程式設計手冊)Interfaces (C# Programming Guide)

介面包含類別struct 可實作的一組相關功能定義。An interface contains definitions for a group of related functionalities that a class or a struct can implement.

例如,您可以藉由使用介面,在類別中包含多個來源的行為。By using interfaces, you can, for example, include behavior from multiple sources in a class. 這項功能在 C# 中是很重要的,因為語言不支援類別的多重繼承。That capability is important in C# because the language doesn't support multiple inheritance of classes. 此外,如果您要模擬結構繼承,則必須使用介面,因為它們實際上無法繼承自另一個結構或類別。In addition, you must use an interface if you want to simulate inheritance for structs, because they can't actually inherit from another struct or class.

您會使用 interface 關鍵字來定義介面,You define an interface by using the interface keyword. 如下列範例所示。as the following example shows.

interface IEquatable<T>
{
    bool Equals(T obj);
}

結構的名稱必須是有效的 C# 識別碼名稱The name of the struct must be a valid C# identifier name. 依慣例,介面名稱的開頭是大寫的|capital IBy convention, interface names begin with a capital I.

任何實作 IEquatable<T> 介面的類別或結構,必須包含 Equals 方法的定義,該方法符合介面指定的簽章。Any class or struct that implements the IEquatable<T> interface must contain a definition for an Equals method that matches the signature that the interface specifies. 如此一來,您可以倚賴實作 IEquatable<T> 的類別,以包含 Equals 方法,類別的執行個體可以使用該方法判斷它是否等於相同類別的另一個執行個體。As a result, you can count on a class that implements IEquatable<T> to contain an Equals method with which an instance of the class can determine whether it's equal to another instance of the same class.

IEquatable<T> 的定義不提供 Equals 的實作。The definition of IEquatable<T> doesn’t provide an implementation for Equals. 介面只會定義簽章。The interface defines only the signature. 如此一來,在 C# 中的介面類似於抽象類別,其中的所有方法都是抽象的。In that way, an interface in C# is similar to an abstract class in which all the methods are abstract. 不過,類別或結構可以實作多個介面,但類別只會繼承單一類別抽象或不繼承。However, a class or struct can implement multiple interfaces, but a class can inherit only a single class, abstract or not.

如需抽象類別的詳細資訊,請參閱抽象和密封類別以及類別成員For more information about abstract classes, see Abstract and Sealed Classes and Class Members.

介面可以包含方法、屬性、事件、索引子,或以上四個成員類型的組合。Interfaces can contain methods, properties, events, indexers, or any combination of those four member types. 如需範例的連結,請參閱相關章節For links to examples, see Related Sections. 介面不能包含常數、欄位、運算子、執行個體建構函式、完成項或類型。An interface can't contain constants, fields, operators, instance constructors, finalizers, or types. 介面成員會自動變成公用,且它們不能包含任何存取修飾詞。Interface members are automatically public, and they can't include any access modifiers. 成員也不能是 staticMembers also can't be static.

若要實作介面成員,實作類別的對應成員必須是公用、非靜態,且具有與介面成員相同的名稱和簽章。To implement an interface member, the corresponding member of the implementing class must be public, non-static, and have the same name and signature as the interface member.

當類別或結構實作介面時,類別或結構必須提供介面定義之所有成員的實作。When a class or struct implements an interface, the class or struct must provide an implementation for all of the members that the interface defines. 介面本身不提供功能讓類別或結構可以如同繼承基底類別功能一般繼承。The interface itself provides no functionality that a class or struct can inherit in the way that it can inherit base class functionality. 不過,如果基底類別實作介面,則衍生自基底類別的任何類別都會繼承該實作。However, if a base class implements an interface, any class that's derived from the base class inherits that implementation.

下列範例會示範 IEquatable<T> 介面的實作。The following example shows an implementation of the IEquatable<T> interface. 實作類別 Car 必須提供 Equals 方法的實作。The implementing class, Car, must provide an implementation of the Equals method.

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 car)
    {
        return this.Make == car.Make &&
               this.Model == car.Model &&
               this.Year == car.Year;
    }
}

類別的屬性與索引子可以針對介面中定義的屬性或索引子定義額外的存取子。Properties and indexers of a class can define extra accessors for a property or indexer that's defined in an interface. 例如,介面可能會宣告具有 get 存取子的屬性。For example, an interface might declare a property that has a get accessor. 實作介面的類別可以宣告具有 getset 存取子的相同屬性。The class that implements 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. 如需明確實作的詳細資訊,請參閱明確介面實作介面屬性For more information about explicit implementation, see Explicit Interface Implementation and Interface Properties.

介面可以繼承自其他介面。Interfaces can inherit from other interfaces. 類別可能透過基底類別包含介面多次,繼承或透過其他介面繼承的介面。A class might include an interface multiple times through base classes that it inherits or through interfaces that other interfaces inherit. 不過,類別只能提供介面實作一次,而且只有在類別將介面宣告為類別 (class ClassName : InterfaceName) 定義的一部分時。However, the class can provide an implementation of an interface only one time and only if the class declares the interface as part of the definition of the class (class ClassName : InterfaceName). 如果因為您繼承實作介面的基底類別而繼承介面,則基底類別會提供介面成員的實作。If the interface is inherited because you inherited a base class that implements the interface, the base class provides the implementation of the members of the interface. 不過,衍生的類別可以實作任何虛擬介面成員,而不使用繼承的實作。However, the derived class can reimplement any virtual interface members instead of using the inherited implementation.

基底類別也可以使用虛擬成員來實作介面成員。A base class can also implement interface members by using virtual members. 在此情況下,衍生的類別可以藉由覆寫虛擬成員來變更介面行為。In that case, a derived class can change the interface behavior by overriding the virtual members. 如需虛擬成員的詳細資訊,請參閱多型For more information about virtual members, see Polymorphism.

介面摘要Interfaces summary

介面具有下列屬性:An interface has the following properties:

  • 介面類似只有抽象成員的抽象基底類別。An interface is like an abstract base class with only abstract members. 任何實作介面的類別或結構必須實作它的所有成員。Any class or struct that implements the interface must implement all its members.
  • 介面無法直接具現化。An interface can't be instantiated directly. 其成員是由實作介面的任何類別或結構實作。Its members are implemented by any class or struct that implements the interface.
  • 介面可以包含事件、索引子、方法和屬性。Interfaces can contain events, indexers, methods, and properties.
  • 介面不包含方法的實作。Interfaces contain no implementation of methods.
  • 類別或結構可以實作多個介面。A class or struct can implement multiple interfaces. 類別可以繼承基底類別,也會實作一或多個介面。A class can inherit a base class and also implement one or more interfaces.

本節內容In this section

明確介面實作Explicit Interface Implementation
說明如何建立介面特定的類別成員。Explains how to create a class member that’s specific to an interface.

如何:明確實作介面成員How to: Explicitly Implement Interface Members
提供如何明確實作介面成員的範例。Provides an example of how to explicitly implement members of interfaces.

如何:明確實作兩個介面的成員How to: Explicitly Implement Members of Two Interfaces
提供如何透過繼承明確實作介面成員的範例。Provides an example of how to explicitly implement members of interfaces with inheritance.

相關章節Related Sections

了解 C# 3.0:掌握 C# 3.0 的基本概念中的介面Interfaces in Learning C# 3.0: Master the Fundamentals of C# 3.0

另請參閱See also