![]() An interface can't contain instance fields, instance constructors, or finalizers. ![]() Beginning with C# 11, interface members that aren't fields may be static abstract. Interfaces may contain static constructors, fields, constants, or operators. Interfaces can contain instance methods, properties, events, indexers, or any combination of those four member types. A class or struct can implement multiple interfaces, but a class can only inherit from a single class.įor more information about abstract classes, see Abstract and Sealed Classes and Class Members. The definition of IEquatable doesn't provide an implementation for Equals. As a result, you can count on a class that implements IEquatable 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. By convention, interface names begin with a capital I.Īny class or struct that implements the IEquatable interface must contain a definition for an Equals method that matches the signature that the interface specifies. The name of an interface must be a valid C# identifier name. You define an interface by using the interface keyword as the following example shows. 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. That capability is important in C# because the language doesn't support multiple inheritance of classes. An interface may not declare instance data such as fields, auto-implemented properties, or property-like events.īy using interfaces, you can, for example, include behavior from multiple sources in a class. An interface may define a default implementation for members. An interface may define static methods, which must have an implementation. An interface contains definitions for a group of related functionalities that a non-abstract class or a struct must implement.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |