C # interfaces and abstract classes: Interface and abstract

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.