Talk about interfaces in C #

Source: Internet
Author: User

Related Statements of interfaces
1. An interface defines a contract.

2. Interfaces can accommodate methods, C # Properties, events, and indexers.

3. In an interface declaration, we can declare 0 or more members.

4. The default access type for all interface members is public.

5. If any modifiers are included in the interface member declaration, a compiler error is generated.

6. Like a non-abstract class, an abstract class must provide an implementation of all members of an interface, as long as those members appear in the base class of the class.


Understanding of the interface
1. Interface-oriented programming uses a basic nature of OO-polymorphism, the same method of different manifestations. Can think of this, the client to write their own program, if directly oriented to a specific class to write programs, then this program has a hint, that the client will be affected, but if facing an interface is different, a specific class changed, only know the interface, A specific class of client can be completely fixed. It is said that the top leadership is better when, because the things that can be done usually for the common people is virtual, the more virtual is not easy wrong.
This principle is also applicable in OO.

2. A different perspective, interface-oriented programming reflects another aspect of OO-encapsulation, the interface will be specific implementation encapsulated, can not affect the customer's situation of the switch implementation

3. The function of the interface, Word, is the category of the Flag class (Type of Class). By attributing different types of classes to different interfaces, you can better manage them. The essence of Oo, I think, is the abstract of the object, the most can reflect this is the interface. Why we discuss design patterns are only for the language with the ability to abstract (such as C + +, Java, C #, etc.), because the design pattern of the study, in fact, is how to reasonably abstract. (Cowboy's famous saying is "Abstraction is the part of the drawing of the image", seemingly ridicule, rationale).


Use of an empty interface
When the interface is used, there are 2 scenarios for an empty interface:
1. Similar to the ibuilderpolicy in ObjectBuilder, they often make a mark, indicating that a feature is needed. Of course you can also use this to indicate that your class has a feature that implements one of your interfaces.

NamespaceMicrosoft.Practices.ObjectBuilder

{

///

Represents Abuilder policy interface. Since there is no fixed requirements

Forpolicies, it acts as a marker interface from which to derive all other

Policyinterfaces.

///

Public Interfaceibuilderpolicy

{

}

}


2. Your interface inherits other interfaces (not NULL), and your interface itself does not declare functions. This is generally because you do not want users to use the parent interface as the parameter type, because their purpose may be different, this time can be implemented with an empty interface.


Interface Text

{

Stringgettext ();

}


Interface Sqltext:text

{


}

As you can see, the text interface is used to return a string. While SQLText is an empty interface, it inherits the text interface. That is to say, SQLText is also a text. But we can know that any one string is not necessarily a SQL string, So at this point, a SQLTEXT interface is declared to be used for the table name the current string is an SQL string. Your function can be declared like this:

Public Voiddoquery (SQLText sqltext)

Rather than this:

Public voiddoquery (text text)

Avoid the user to create the idea of ambiguity, at a glance, it is clear that a SQL string should be passed in.

Why does the member of the interface not have a delegate
We all know that C # interface can contain events, in fact, when we see the event, it is easy to think of the delegate, the delegate is the basis of the event, if the delegates and events are not particularly clear to the programmer will not understand why the C # interface can contain events and cannot have delegates. In fact, the simple argument is that the delegate is also a type, the delegate keyword introduces a new type, so a C # interface cannot contain a delegate and treat it as a member, whereas the Event keyword introduces a new member, so events can be attributed to the human interface. To understand this, from the mission of the C # interface, the C # interface is a contract that regulates the behavior of the interface's implementation, rather than what is needed. Very simple, for example, "Party member" is an interface, it must have an action is "serving the person", "so-and-so party member" to achieve "party member" This interface, then "a party member" must also "serve the person", as for You "a party member" whether must embrace "computer", "child". Then the "party member" in this interface will certainly not have provisions. This is the purpose of the interface, to standardize the implementation of some of the behavior. So the members of the C # interface are methods and there will be no other. A little C # common sense programmers understand that the properties in C # are actually two methods, a set method, a Get method, the same event and indexer are all methods, see the following interface:

Public Interfaceidrawingobject

{

Eventeventhandler OnDraw;

String Name

{

Get

Set

}

int This[intindex]

{

Get

Set

}

void SetValue ();

}

This interface contains all the members, events, properties, indexers, methods that the C # interface can accept. After compiling the interface, we use the Msildisassembler tool to see:

This is understood, in fact the property Name corresponds to Get_name (), Set_name () The two methods, the event OnDraw corresponds to the Add_ondraw (), Remove_ondraw () These two methods, the indexer corresponds to the get_item (), Set_item () These two methods. Look at the definitions of the following delegates and classes:

Public delegatevoid testeventdelegate (object sender, System.EventArgs e);

Class TestClass

{

Public Voidsetvalue ()

{ }

}

See, there's no difference between defining a delegate and defining a class, all defining a new type. So the C # interface cannot have a delegate, unless Microsoft tells us that a class can be defined in the C # interface.

Talk about interfaces in C #

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.