C #-based interface basic tutorial II

Source: Internet
Author: User
Section 2 Interface Definition

Technically, an interface is a set of data structures that contain functional methods. Through this set of data structures, the customerCodeYou can call the functions of component objects.

The interface is defined in the following format:

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


1. attributes (optional): additional definition information.

2. modifiers (optional): allowed modifiers include new and four access modifiers. New, public, protected, internal, and private. In an interface definition, the same modifier is not allowed to appear multiple times. The new modifier can only appear in the nested interface, indicating that the inherited members with the same name are overwritten. The public, protected, internal, and private modifiers define access permissions for interfaces.

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.

6. Interface-body: defines interface members.

7. An interface can be a member of a namespace or class and contain the signature of the following members: method, attribute, and indexer.

8. An interface can be inherited from one or more basic interfaces.

The interface concept is very similar in C # and Java. The key word of an interface is interface. An interface can expand one or more other interfaces. By convention, the interface name starts with an uppercase letter "I. The following code is an example of the C # interface, which is exactly the same as the interface in Java:

Interface ishape {
Void draw ();

If you derive from two or more interfaces, the names of the parent interfaces are separated by commas, as shown in the following code:

Interface inewinterface: iparent1, iparent2 {}

However, unlike Java, interfaces in C # cannot contain fields ). In addition, in C #, all methods in the interface are public methods by default. In Java, a method definition can carry a public modifier (even if this is not necessary), but in C #, explicitly specifying a public modifier for an interface method is invalid. For example, the following C # interface will generate a compilation error.

Interface ishape {public void draw ();}

The following example defines an interface named iControl, which contains a member method "paint:

Interface iControl {
Void paint ();

In the following example, the interface iinterface inherits from two basic interfaces, ibase1 and ibase2:

Interface iinterface: ibase1, ibase2 {
Void Method1 ();
Void method2 ();

Interfaces can be implemented by classes. 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 includes both the base class and interface, the base class appears first in the list. 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 ();

An object cannot be instantiated from this definition, but a class can be derived from it. Therefore, this class must implement the showmyface abstract method:

Class cface: iface
Public void showmyface (){
Console. writeline ("Implementation ");
Basic Interface

An interface can be inherited from zero or multiple interfaces, which are the explicit basic interfaces called this interface. When an interface has more than zero explicit basic interfaces, the form in the interface definition is that the interface identifier is followed by a colon ":" and a comma ", "List of separated basic interface identifiers.

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 incorrect to specify a private or internal interface in the basic interface of a public interface.

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

3. The base interfaces of interfaces are both explicit base interfaces and their base interfaces. In other words, the set of basic interfaces is completely composed of the explicit basic interfaces and their explicit basic interfaces. 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 basic interfaces of icombobox are iControl, itextbox, and ilistbox.

4. An interface inherits all the members of its basic interface. In other words, the above interface icombobox inherits the settext and setitems members just like painting.

5. A class or structure that implements interfaces also implicitly implements the basic interfaces of all interfaces.

Interface subject

The interface subject of an interface defines the interface members.


Defining an interface is mainly an interface member. Please refer to the next section-defining an interface member.

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.