C #-based interface basic tutorial 3

Source: Internet
Author: User
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.

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.