Display implementation interface and implementation Interface

Source: Internet
Author: User

Display implementation interface and implementation Interface

The interface defines a series of behavior specifications, defining a typeCan-Do. For example, the implementation of the IEnumerable interface defines the GetEnumerator method, used to obtain an enumerative number, which supports iteration in the set, that is, the foreach we often call. An interface only defines behaviors. The specific implementation needs to be the responsibility of specific types. The methods for implementing interfaces are dividedImplicit implementation and display implementation.

1. Implementation Interface Method of Implicit/display

In short, we usually use an implicit implementation method by default. For example:

Interface ILog {void Log ();} public class FileLogger: ILog {public void Log () {Console. WriteLine ("records to files! ");}}

Implicit implementation is very simple. We usually agree that the interface name should start with I for ease of reading. The methods in the interface do not need to be public, and the compiler will automatically add them. The method that implements the interface in the type can only be public, or be defined as a virtual method, which is overwritten by the subclass. The display implementation method is as follows:

Public class EventLogger: ILog {void ILog. Log () {Console. WriteLine ("records system events! ");}}

Different from the above, the method is specified using ILog, and there is no (or not) public or private modifier.

In addition to syntax differences, the call method is also different. The display implementation can only be called using variables of the interface type, such:

FileLogger fileLogger = new FileLogger (); fileLogger. log (); // The correct EventLogger eventLogger = new EventLogger (); eventLogger. log (); // error ILog log = new EventLogger (); log. log (); // correct

Ii. When to use

1. c # multiple interfaces can be implemented. If multiple interfaces define the same method, they can be distinguished by display implementation. For example:

 public class EmailLogger : ILog, ISendable{    void ILog.Log()    {        Console.WriteLine("ILog");    }     void ISendable.Log()    {        Console.WriteLine("ISendable");    }}

2. Enhanced type security during compilation and avoided Value Type Packing

With generics, We can naturally ensure the type security during compilation and avoid the Value Type Packing operation. But sometimes there may be no corresponding generic version. For example, IComparable (here is just an example, actually there is IComparable <T> ). For example:

Interface IComparable {int CompareTo (object obj);} struct ValueType: IComparable {private int x; public ValueType (int x) {this. x = x;} public int CompareTo (object obj) {return this. x-(ValueType) obj ). x ;}// call: ValueType vt1 = new ValueType (1); ValueType vt2 = new ValueType (2); Console. writeLine (vt1.CompareTo (vt2 ));

Because the parameter is an object, CompareTo will be boxed, And the type security during compilation cannot be obtained. For example, we can upload a string without any errors during compilation, and the InvalidCastException will not be thrown until it is run. Use the display method to implement the interface, such:

public int CompareTo(ValueType vt){    return this.x - vt.x;} int IComparable.CompareTo(object obj){    return CompareTo((ValueType)obj);}  

If you execute the above Code again, the packing operation will not occur, and you can obtain the type security during compilation. However, if we call the interface variable, the packaging will occur again and the type security detection capability during compilation will be lost.

IComparable vt1 = new ValueType (1); // bind ValueType vt2 = new ValueType (2); Console. WriteLine (vt1.CompareTo (vt2); // bind again

Iii. Disadvantages

1. The display implementation can only be called using interface type variables. It will give people the feeling that a certain type has implemented this interface but cannot call methods in the interface. In particular, when writing a class library for calls to others, the interface method of display implementation is not displayed in vs press f12. (Someone asked csdn why an interface method is not required for a type)

2. For the value type, to call the display implementation method, the packing operation will occur.

3. the quilt class cannot be inherited and used.

Provided by "figure douluo"

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.