Introduction to interfaces in C #

Source: Internet
Author: User

Interface statement

  1. An interface defines a contract.
  2. The interface can accommodate methods, C # attributes, events, and indexers.
  3. In an interface declaration, we can declare zero or multiple members.
  4. The default access type of all interface members is public.
  5. If any modifier is included in the interface member declaration, a compiler error is generated.
  6. Similar to a non-abstract class, an abstract class must provide the implementation of all the Members in the interface, as long as these Members appear in the base class of this class.

Interface understanding

Interface-Oriented Programming utilizes a Basic Property of OO-polymorphism, which is different in the same method. You can think about this. When the client is writing its own program, if it is directly oriented to a specific class Write Program, the client will be affected, however, if an interface is different, a specific class changes, only the interface is known, and the client of the specific class can be completely unchanged. It is said that upper-level leaders are better at doing things, because what they can do is usually virtual for common people. The more virtual, the more difficult it is to make mistakes.

This principle is also applicable in OO.

From another perspective, interface-oriented programming reflects another aspect of OO-encapsulation. The specific implementation of the interface is encapsulated, so that the implementation can be switched without affecting the customer.

The function of an interface, in a word, is the type of class ). You can better manage different types of classes by assigning them to different interfaces. The essence of OO, I think, is the abstraction of objects, the interface that best reflects this point is. Why do we discuss that the design patterns are only for languages with abstract capabilities (such as c ++, java, and c #) because the design patterns are studied, it is actually how to reasonably abstract. (Cowboy's famous saying is that "abstraction is the part of image extraction". It seems ridiculous, but it is actually the most reasonable ).

Use of null Interfaces

When an interface is used, there are two scenarios for an empty interface:

1. Similar to the IBuilderPolicy in ObjectBuilder, they often make a tag to indicate that a function is required. Of course, you can also use this to indicate that your class has a certain function and implements one of your interfaces.

Namespace Microsoft. Practices. ObjectBuilder
{
/// <Summary>
/// Represents a builder policy interface. Since there are no fixed requirements
/// For policies, it acts as a marker interface from which to derive all other
/// Policy interfaces.
/// </Summary>
Public interface IBuilderPolicy
{
}
}

2. Your interface inherits other interfaces (not empty), and your interface itself does not declare a function. In this case, you generally do not want the user to use the parent interface as the parameter type, because they may have different purposes. In this case, you can use an empty interface.

Interface Text
{
String getText ();
}
 
Interface SqlText: Text
{
 
}

As you can see, the Text interface is used to return a string. SqlText is an empty interface that inherits the Text interface. That is to say, SqlText is also a type of Text. However, we can know that any string is not necessarily an SQL string. Therefore, a SqlText interface is declared to use the current string of the table name as an SQL string. Your function can be declared as follows:

Public void doQuery (SqlText sqlText)

Instead:

Public void doQuery (Text text)

To avoid ambiguity, you can see at a glance that an SQL string should be passed in.

Why is the interface member not commissioned?

We all know that the C # interface can contain events. In fact, when we see an event, we can easily think of delegation, which is the basis of the event, programmers who are not particularly clear about delegation and events will not understand why C # interfaces can contain events rather than delegation. In fact, the simple saying is that the delegate is also a type. The delegate keyword introduces a new type, so a C # interface cannot tolerate a delegate and treat it as a member; the event keyword introduces a new member, so the event can be a human interface. To understand this, we should start with the mission of the C # interface. The C # interface is a contract that standardizes the behavior of the interface implementer, rather than something. It is very simple. For example, a "Party member" is an interface, and it must have an action to "serve the people". A "XX Party member" implements the "Party member" interface, then, "XX party members" must also "serve the people". As to whether you "XX party members" must support "computers" and "children ". Therefore, the "Party member" interface certainly does not have provisions. This is the purpose of the interface and standardizes some actions of the implementer. Therefore, all the members of the C # interface are methods. Programmers who have a little knowledge about C # understand that the attributes in C # are actually two methods: One Set method, one Get method, and the same event and indexer, see the following interface:

Public interface IDrawingObject
{
Event EventHandler OnDraw;

String Name
{
Get;
Set;
}

Int this [int index]
{
Get;
Set;
}

Void SetValue ();
}

This interface contains all the Members, events, attributes, indexers, and methods that the C # interface can accept. After compiling this interface, we can use the MSIL Disassembler tool to check it:

Now everyone understands that the attribute Name corresponds to the Get_Name () and Set_Name () methods. The event OnDraw corresponds to the add_OnDraw () and remove_OnDraw () methods, the indexer corresponds to the get_Item () and set_Item () methods. See the following delegate and class definitions:

Public delegate void TestEventDelegate (object sender, System. EventArgs e );
Class TestClass
{
Public void SetValue ()
{
}
}

As you can see, defining a delegate is no different from defining a class, and a new type is defined. Therefore, the C # interface cannot be delegated unless Microsoft tells us that the C # interface can define classes.
From: http://www.cftea.com/c/2009/02/83HTWD161NBEMFRS.asp

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.