Movatterモバイル変換


[0]ホーム

URL:


Skip to main content

This browser is no longer supported.

Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.

Download Microsoft EdgeMore info about Internet Explorer and Microsoft Edge
Table of contentsExit focus mode

Interfaces - define behavior for multiple types

  • 2023-03-18
Feedback

In this article

An interface contains definitions for a group of related functionalities that a non-abstractclass or astruct must implement. An interface may definestatic methods, which must have an implementation. An interface may define a default implementation for members. An interface may not declare instance data such as fields, automatically implemented properties, or property-like events.

By using interfaces, you can, for example, include behavior from multiple sources in a class. 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.

You define an interface by using theinterface keyword as the following example shows.

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

The name of an interface must be a valid C#identifier name. By convention, interface names begin with a capitalI.

Any class or struct that implements theIEquatable<T> interface must contain a definition for anEquals method that matches the signature that the interface specifies. As a result, you can count on a class of typeT that implementsIEquatable<T> to contain anEquals method with which an instance of this class can determine whether it's equal to another instance of the same class.

The definition ofIEquatable<T> doesn't provide an implementation forEquals. A class or struct can implement multiple interfaces, but a class can only inherit from a single class.

For more information about abstract classes, seeAbstract and Sealed Classes and Class Members.

Interfaces can contain instance methods, properties, events, indexers, or any combination of those four member types. Interfaces may contain static constructors, fields, constants, or operators. Beginning with C# 11, interface members that aren't fields may bestatic abstract. An interface can't contain instance fields, instance constructors, or finalizers. Interface members are public by default, and you can explicitly specify accessibility modifiers, such aspublic,protected,internal,private,protected internal, orprivate protected. Aprivate member must have a default implementation.

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.

Note

When an interface declares static members, a type implementing that interface may also declare static members with the same signature. Those are distinct and uniquely identified by the type declaring the member. The static member declared in a typedoesn't override the static member declared in the interface.

A class or struct that implements an interface must provide an implementation for all declared members without a default implementation provided by the interface. However, if a base class implements an interface, any class that's derived from the base class inherits that implementation.

The following example shows an implementation of theIEquatable<T> interface. The implementing class,Car, must provide an implementation of theEquals 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, this.Model, this.Year) ==            (car?.Make, car?.Model, car?.Year);    }}

Properties and indexers of a class can define extra accessors for a property or indexer that's defined in an interface. For example, an interface might declare a property that has aget accessor. The class that implements the interface can declare the same property with both aget andset accessor. However, if the property or indexer uses explicit implementation, the accessors must match. For more information about explicit implementation, seeExplicit Interface Implementation andInterface Properties.

Interfaces can inherit from one or more interfaces. The derived interface inherits the members from its base interfaces. A class that implements a derived interface must implement all members in the derived interface, including all members of the derived interface's base interfaces. That class may be implicitly converted to the derived interface or any of its base interfaces. A class might include an interface multiple times through base classes that it inherits or through interfaces that other interfaces inherit. 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. When interfaces declare a default implementation of a method, any class implementing that interface inherits that implementation (You need to cast the class instance to the interface type to access the default implementation on the Interface member).

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, seePolymorphism.

Interfaces summary

An interface has the following properties:

  • In C# versions earlier than 8.0, an interface is like an abstract base class with only abstract members. A class or struct that implements the interface must implement all its members.
  • Beginning with C# 8.0, an interface may define default implementations for some or all of its members. A class or struct that implements the interface doesn't have to implement members that have default implementations. For more information, seedefault interface methods.
  • An interface can't be instantiated directly. Its members are implemented by any class or struct that implements the interface.
  • A class or struct can implement multiple interfaces. A class can inherit a base class and also implement one or more interfaces.
Collaborate with us on GitHub
The source for this content can be found on GitHub, where you can also create and review issues and pull requests. For more information, seeour contributor guide.

Feedback

Was this page helpful?

YesNo

In this article

Was this page helpful?

YesNo