I. Interface
The interface is a common tool in C #. I will not talk about the concept or anything. Here are some notes:
1. The interface can only contain declarations of functions, attributes, and events:
Interface IParent
{
Void Show ();
String Type
{
Get;
Set;
}
Event AddChildren Add;
}
Members declared in the interface do not need access modifiers (public, private, etc.), because the interface member permissions are all public by default, in addition, it is worth noting that the event can be declared in the interface because the event is the special attribute of the delegate.
The interface cannot be static, or be declared as an instance member. The following statement is incorrect:
Static interface IParent
{
Static void Show ();
String Type
{
Get;
Set;
}
Event AddChildren Add;
}
You will be prompted:
Error 1 modifier "static" is invalid for this item C: statements and SettingsHMD Desktop \ CLR_ExciseCLR_ExciseLib.cs 10 22 CLR_Excise
The reason is that if the interface is static, it cannot be inherited, and the static members cannot be directly accessed by the interface (because there are no objects in the interface, but only declarations), so the definition is meaningless in the interface.
2. Some declarations are supported for the interface:
The above interface can be declared as three parts in a namespace:
Partial interface IParent
{
Void Show ();
}
Partial interface IParent
{
String Type
{
Get;
Set;
}
}
Partial interface IParent
{
Event AddChildren Add;
}
The declaration method of some of the above interfaces and the effect of the same interface are identical. For details about some keywords, see the role of the partial keyword in C # (excerpt)
3. Interface implementation:
Interface members can be implemented in two ways:
<1> implicit implementation:
Class ChildrenA: IParent
{
# Region IParent Member
Public void Show ()
{
Throw new NotImplementedException ();
}
Public string Type
{
Get
{
Throw new NotImplementedException ();
}
Set