Section 3 defines interface members
An interface can contain one or more members. These members can be methods, attributes, index indicators, and events, but cannot be constants, fields, operators, constructors, or destructor, and cannot contain any static members. The interface definition creates a new definition space, and the interface definition directly includes the interface member definition to introduce the new member into the definition space.
Note:
1. The interface member is a member inherited from the basic interface and a member defined by the interface itself.
2. The interface definition can be zero or multiple members. The interface must be a method, attribute, event, or indexer. An interface cannot contain constants, fields, operators, instance constructors, destructor, or types, nor static members of any type.
3. Define an interface that contains a method, attribute, event, and indexer for each possible type of member.
4. The default access method for interface members is public. The interface member definition cannot contain any modifier. For example, the abstract, public, protected, internal, private, virtual, override, or static modifier cannot be added before the member definition.
5. The interface members cannot have the same names. The inherited members do not need to be defined, but the interface can define members with the same name as the inherited members. In this case, we say that the interface members overwrite the inherited members, which will not cause errors, but the compiler will give a warning. To disable the warning prompt, add a new keyword before the member definition. However, if the parent interface member is not overwritten, using the new keyword will cause the compiler to issue a warning.
6. The method name must be different from the names of all attributes and events defined in the same interface. In addition, the method signature must be different from the signature of all other methods defined in the same interface.
7. The attribute or event name must be different from the name of all other Members defined in the same interface.
8. The signature of one indexer must be different from that of all other indexers defined in the same interface.
9. attributes in the interface method declaration, return-type, identifier, and formal-parameter-lis) it has the same meaning as those in the method declaration of a class. An interface method declaration does not allow a method subject to be specified, but usually ends with a semicolon.
10. The access character of the interface attribute declaration corresponds to the access character of the Class Attribute declaration, except that the access character subject must usually use a semicolon. Therefore, whether the attribute is read/write, read-only, or write-only, the access character is completely determined.
11. attributes, types, and formal-parameter-list in the interface index Declaration have the same meaning as those declared by the class index.
In the following example, the interface IMyTest contains the index indicator, event E, method F, and attribute P:
Interface IMyTest {
String this [int index] {get; set ;}
Event EventHandler E;
Void F (int value );
String P {get; set ;}
}
Public delegate void EventHandler (object sender, EventArgs e );
In the following example, the IStringList Interface contains an interface for each possible type of members: A method, an attribute, an event, and an index.
Public delegate void StringListEvent (IStringList sender );
Public interface IStringList
{
Void Add (string s );
Int Count {get ;}
Event StringListEvent Changed;
String this [int index] {get; set ;}
}
Full name of the interface member
You can also use fully qualified name ). The full name of the interface is composed of this. The interface name is followed by the dot "." And the member name, for example, for the following two interfaces:
Interface IControl {
Void Paint ();
}
Interface ITextBox: IControl {
Void GetText (string text );
}
Here, the full name of Paint is IControl. Paint, and the full name of GetText is ITextBox. GetText. Of course, the member names in the full name must have been defined in the interface. For example, it is unreasonable to use ITextBox. Paint.
If an interface is a namespace Member, its full name must also contain the namespace name.
Namespace System
{
Public interface IDataTable {
Object Clone ();
}
}
The full name of the Clone method is System. IDataTable. Clone.
After the interface is defined, the next step is how to access the interface. Please refer to the next section -- access interface