C # Interface Simple Introduction

Source: Internet
Author: User

Reprint: http://www.cnblogs.com/jara/p/3450493.htmlAnalysis of C # Interface features and practical application

How does the mastery of C # Interface (interface) work for our development? How can the use of the C # interface improve our programs? So let's start by looking at C # interface features and specific example usage analysis:

    • C # interface feature 1: All "virtual" cannot be instantiated, which is why the interface cannot contain a field-member variable
    • C # Interface features 2: Just because the interface is virtual, so that the interface within the index, properties, time, etc. can only be declared, and can not be implemented within the interface, specifically how to implement is a derived interface or derived classes.
    • C # Interface features 3: All have the nature of the template, if an interface or class inherits from an interface, it will automatically have the characteristics of the integrator (including indexes, attributes, functions, practices, etc.).
    • C # Interface features 4: The interface supports multiple inheritance, whereas in C #, the class supports single inheritance, and the interface actually represents a load capacity.

Here is a simple definition of a C # interface instance:

Interface SampInterface1  {      string       this[int index]      {          get;          Set     }      ;      Event EventHandler event;      void  Find (int value);      Note there is no      {      }            string Po      int    {          get;          Set     }      ;  

The above C # interface instance defines an index of this, a practice event, a method of find and a property point.

public interface sampineterface:sampineterface1{   pravite int a=1;  void find (int value)  {  a+=value;  }  Event EventHandler event;  protected void OnEvent ()  {  if (event=null)  {return Event (this. System.EventAgrs.Empty;)}  }   1}
a detailed definition of the C # interface

The definition of C # interfaces Technically, an interface is a set of data structures that contain a functional approach. With this set of data structures, the client code can invoke the functionality of the Component object.

The general form of the C # interface definition is:

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

Description of the C # interface definition:

attributes (optional): additional defined information.

modifiers (optional): The modifier that is allowed to be used has a 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.

· Indicators and events.

identifier: interface name.

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

interface-body: definition of an interface member.

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

· 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.

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");    }   

The base interface for the definition of C # interfaces

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 for the definition of C # interface:

Interface Type list Description:

    • An explicit base interface for 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.
    • It is wrong for an interface to inherit directly or indirectly from itself.
    • The base interfaces of an interface are both explicit base interfaces and their base interfaces. 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.

    • 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.
    • A class or struct that implements an interface implicitly implements the base interface for all interfaces.

Interface body for the definition of C # interface

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

Interface-body:  {   
A detailed C # interface and implementation

What is a C # interface? The C # Interface (interface) is used to define a protocol for a program. The class or structure that implements the interface is strictly consistent with the definition of the interface. With this agreement, you can throw away the limitations of programming languages (theoretically). C # interfaces can inherit from multiple base interfaces, and classes or structs can implement multiple interfaces. C # interfaces can contain methods, properties, events, and indexers. The interface itself does not provide an implementation of the members it defines. The interface specifies only the members that the class or interface that implements the interface must provide.

The C # interface is like a template that defines the methods that an object must implement to enable these methods to be referenced as interface instances. The interface cannot be instantiated. A class can implement multiple interfaces and be indexed through these implemented interfaces. An interface variable can only index an instance of a class that implements the interface. Example:

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

The C # interface in the example above contains an index of this, an event even, a method find, and a property point. C # interfaces can support multiple inheritance. As in the following example, the interface "IComboBox" inherits from "ITextBox" and "IListBox" at the same time.

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 instantiate a C # interface in multiple instances. As in the following example, the class "EditBox" Inherits the class "Control" while inheriting from "IDataBound" and "IControl".

Interface IDataBound  {  void Bind (Binder b);  }  Blic 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 is implemented from the "IDataBound" interface as "public" in the "EditBox" class.

a summary description of the C # interface:

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

2, interfaces and classes can inherit multiple interfaces.

3, a class can inherit a base class, and an interface cannot inherit a class at all. This model avoids the multiple inheritance problem of C + +, and the implementation of different base classes in C + + may conflict. Therefore, complex mechanisms such as virtual inheritance and explicit scopes are no longer needed. The simplified interface model of C # helps 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 there is no execution code at all. This implies that an interface cannot be instantiated, and only one object derived from that interface can be instantiated.

5. Interfaces can define methods, properties, and indexes. So, in contrast to a class, the particularity of an interface is that when a class is defined, it can be derived from a multiple interface, and you can only derive from one of the only classes.

C # Interface Simple Introduction

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.