One of the basic course of interface based on C #

Source: Internet
Author: User
Tags abstract define bind implement inheritance interface string

First section Interface overview

interface (interface) is used to define a protocol for a program. The class or struct that implements the interface is strictly consistent with the definition of the interface. With this agreement, the limitations of the programming language (theoretically) can be thrown away. An interface can inherit from multiple base interfaces, and a class or struct can implement multiple interfaces. Interfaces can contain methods, properties, events, and indexers. The interface itself does not provide implementations of the members that it defines. interface specifies only the members that the class or interface that implements the interface must provide.

an interface is like a template that defines the methods that an object must implement in order for these methods to be referenced as interface instances. interface cannot be instantiated. Classes can implement multiple interfaces and are indexed through the interfaces of these implementations. An interface variable can only index an instance of the class that implements the interface. Example:

Interface Imyexample {
String This[int index] {get; set;}
Event EventHandler even;
void find (int value);
String point {get; set;}
}
public delegate void EventHandler (object sender, Event e);

the interface in the example above contains an index this, an event even, a method find, and a property point.

interfaces can support multiple inheritance. As in the following example, the interface "IComboBox" inherits from both "ITextBox" and "IListBox".

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

classes and structs can have multiple instantiations of interfaces. As in the following example, the class "EditBox" Inherits the class "control" and inherits from "IDataBound" and "IControl".

Interface IDataBound {
void Bind (Binder b);
}
public class Editbox:control, IControl, IDataBound {
public void Paint ();
public void Bind (Binder b) {...}

In the above code, the "Paint" method comes from the "IControl" interface, and the Bind method comes from the "IDataBound" interface and is implemented as "public" in the "EditBox" class.

Description:

1, the interfaces in C # are defined independently of the class. This is opposed to the C + + model, where interfaces are actually abstract base classes.

2. Interfaces and classes can inherit multiple interfaces.

3, and the class can inherit a base class, the interface cannot inherit the class at all. This model avoids multiple inheritance problems in C + +, and implementations in different base classes in C + + may conflict. Therefore, complex mechanisms such as virtual inheritance and explicit scopes are no longer needed. C # 's simplified interface model helps to accelerate application development.

4, an interface defines a reference type that has only abstract members. What an interface in C # actually does, there is only a method flag, but no code is executed at all. This implies that an interface cannot be instantiated, and only an object derived from that interface can be instantiated.

5. Interfaces can define methods, properties, and indexes. So, comparing a class, the interface is specific: When you define a class, you can derive from multiple interfaces, and you can derive from only one class.

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.