C # Interface Basics Tutorial two defining interfaces

Source: Internet
Author: User
Tags modifiers

Defining interfaces

Technically, an interface is a set of data structures that contain a function-based approach. With this set of data structures, the client code can invoke the functionality of the Component object.

The general form of defining an interface is:

[Attributes] [modifiers] interface identifier [: Base-list] {interface-body}[;]

Description

1. Attributes (optional): additional defined information.

2. Modifiers (optional): The modifier that is allowed to use has new and four access modifiers. Respectively: New, public, protected, internal, private. The same modifier is not allowed to appear more than once in an interface definition, and the new modifier can only appear in a nested interface, overwriting the inherited member with the same name. The public, protected, internal, and private modifiers define access rights to the interface.

3, indicators and events.

4, Identifier: interface name.

5. Base-list (optional): Contains a list of one or more explicit base interfaces, separated by commas between the interfaces.

6, Interface-body: Definition of interface members.

7. An interface can be a member of a namespace or class and can contain signatures for the following members: Methods, properties, indexers.

8, an interface can inherit from one or more base interfaces.

The concept of interfaces is very similar in C # and Java. The key word of an interface is interface, an interface that can extend one or more other interfaces. By convention, the name of the interface begins with the capital letter "I". The following code is an example of the C # interface, which is identical to the interface in Java:

Interface IShape {
void Draw ();
}

If you derive from two or more two interfaces, the name list of the parent interface is separated by commas, as shown in the following code:

Interface Inewinterface:iparent1, IParent2 {}

However, unlike Java, an interface in C # cannot contain a domain (field). Also note that in C #, all methods within an interface are public methods by default. In Java, a method definition can have a public modifier (even if this is not necessary), but in C #, it is illegal to explicitly specify the public modifier for the method of an interface. For example, the following C # interface will produce a compilation error.

Interface IShape {public void Draw ();}

The following example defines an interface named IControl that contains a member method paint:

Interface IControl {
void Paint ();
}

In the following example, the interface IInterface inherits from the two base interfaces IBase1 and IBase2:

Interface Iinterface:ibase1, IBase2 {
void Method1 ();
void Method2 ();
}

An interface can be implemented by a class. The identifier of the implemented interface appears in the base list of the class. For example:

Class Class1:iface1, Iface2 {
Class member.
}

When the base list of a class contains both a base class and an interface, the first occurrence in the list is the base class. For example:

Class Classa:baseclass, Iface1, Iface2 {
Class member.
}

The following code snippet defines the interface Iface, which has only one method:

Interface IFace {
void Showmyface ();
}

You cannot instantiate an object from this definition, but you can derive a class from it. Therefore, the class must implement the Showmyface abstract method:

Class Cface:iface
{
public void Showmyface () {
Console.WriteLine ("Implementation");
}
}

Base interface

An interface can inherit from zero or more interfaces, which are known as explicit base interfaces for this interface. When an interface has an explicit base interface of more than 0, then in the definition of the interface, the interface identifier is followed by a colon ":" and a list of the base interface identifiers separated by a comma ",".

Interface Base:

: interface Type list Description:

1. An explicit base interface of an interface must be at least as accessible as the interface itself. For example, it is wrong to specify a private or internal interface in the base interface of a public interface.

2. It is wrong for an interface to inherit directly or indirectly from itself.

3, the interface's base interface is the explicit base interface, and is their base interface. In other words, the collection of base interfaces consists entirely of explicit base interfaces and their explicit base interfaces, and so on. In the following example

Interface IControl {
void Paint ();
}
Interface Itextbox:icontrol {
void SetText (string text);
}
Interface Ilistbox:icontrol {
void Setitems (string[] items);
}
Interface Icombobox:itextbox, IListBox {}

The base interfaces of IComboBox are IControl, ITextBox, and IListBox.

4. An interface inherits all the members of its base interface. In other words, the interface above IComboBox inherits members SetText and Setitems just like paint.

5, a class or struct that implements an interface implicitly implements the base interface for all interfaces.

Interface Body

The interface body of an interface defines the members of the interface.

Interface-body:
{interface-member-declarationsopt}

Defining interfaces is primarily defining interface members, see the next section-defining interface members.

C # Interface Basics Tutorial two defining interfaces

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.