Base on C # Interface Basics Tutorial II

Source: Internet
Author: User

Section two defines an interface

Technically, an interface is a set of data structures that contain a function-type method. With this set of data structures, 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 definition of information.

2, modifiers (optional): Allowed modifiers have new and four access modifiers. Respectively are: new, public, protected, internal, private. The same modifier is not allowed to occur more than once in an interface definition, and the new modifier can only appear in nested interfaces, representing members of the same name that have inherited. The public, protected, internal, and private modifiers define the access rights to the interface.

3, indicators and events.

4, Identifier: interface name.

5, Base-list (optional): contains one or more explicit base interface list, the interface is separated by commas.

6, Interface-body: The definition of interface members.

7. An interface can be a member of a namespace or class, and can contain the signatures of the following members: Method, property, indexer.

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

The concept of interfaces is very similar in C # and Java. The keyword of an interface is interface, and an interface 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 exactly the same as the interface in Java:


Interface IShape {
void Draw ();
}

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


Interface Inewinterface:iparent1, IParent2 {}

However, unlike Java, an interface in C # cannot contain fields (field). Also note that in C #, all methods within an interface are by default public methods. In Java, a method definition can have a public modifier (even if this is not necessary), but it is illegal in C # to specify the public modifier for a method that is an interface explicitly. 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 ();
}

Total 2 page: previous 1 [2] Next page



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.