C # interface, do you really understand it?

Source: Internet
Author: User

I. Define Interfaces

Interface to name a set of method signatures. When defining the interface type, you can specify public or internal visibility, but the original statement in [CLR via C #] 3rd is: "when defining the interface type, you can specify any visibility/accessibility (public, protected, internal, etc.) you want. "I personally think this sentence is inappropriate. However, in any case, the accessibility of top-level types not nested in other types can only be internal or public. The default accessibility of these types is internal.

Ii. inherited Interfaces

C # The Compiler requires that the method used to implement an interface be abbreviated as interface method to public. CLR requires that the interface methods be marked as virtual. If the interface methods are not explicitly marked as virtual in the source code, the compiler will mark them as virtual and sealed; this will prevent the derived classes from overwriting the interface methods. If you explicitly mark an interface method as virtual, the compiler will mark this method as virtual and keep it unsealed so that the derived class can override it.

The value type can be zero or multiple interfaces. However, when you convert a value-type instance to an interface type, the value-type instance must be boxed. This is because the interface variable is a reference and must point to an object on the stack so that CLR can check the object type object pointer to determine the exact type of the object. Then, when calling an interface method of the boxed value type, CLR will follow the type object pointer of the object and find the method table of the type object to call the correct method.

Iii. Implementation of implicit and explicit Interfaces

Public class simpletype: idisposable

{

// Implementation of the implicit Interface Method

Public void dispose () {console. writeline ("public dispose ");}

// Implement the explicit interface method (the accessibility cannot be specified, but the accessibility is automatically set to private when the compiler generates the metadata of the method)

Void idisposable. Dispose () {console. writeline ("idisposable dispose ")}

}

 

Public static void main ()

{

Simpletype ST = new simpletype ();

St. Dispose (); // The output result is "public dispose"

Idisposable d = sT;

D. Dispose (); // The output result is "idisposable dispose"

}

Iv. Generic Interfaces

Benefits of generic interfaces:

Private void Method1 ()

{

Int32 x = 1, y = 2;

Icomparable c = X;

C. compareto (y); // compareto expects to receive an object-type parameter. It is no problem to pass y, but the value type is boxed.

C. compareto ("2") // It can be compiled, but an exception is thrown during runtime, which cannot provide type security during compilation.

}

 

Private void method2 ()

{

Int32 x = 1, y = 2;

Icomparable <int32> C = X;

C. compareto (y); // compareto expects to receive an int21 type parameter. It is no problem to pass y, and Y will not be boxed.

C. compareto ("2") // compilation error, which can provide type check during compilation

}

1. Generic interfaces provide excellent type security during compilation.

2. When the value type is processed, the number of packing times is much less.

3. The class can implement the same interface several times, as long as different type parameters are used each time

5. How to Enhance the type security during compilation without the generic interface version?

Public struct somevaluetype1: icomparable

{

Private int32 _ x;

Public somevaluetype (int32 X) {_ x = x ;}

Public int32 compareto (object other)

{

Return (_ x-(somevaluetype) Other). _ x );

}

}

 

Public struct somevaluetype2: icomparable

{

Private int32 _ x;

Public somevaluetype (int32 X) {_ x = x ;}

Public int32 compareto (somevaluetype2 other)

{

Return (_ x-other. _ x );

}

Int32 icomparable. compareto (Object O)

{

Return compareto (somevaluetypes) O );

}

}

 

Public static void main ()

{

Somevaluetype1 V1 = new somevaluetype1 (0 );

Object o = new object ();

V1.compareto (V1); // bind the Value Type

V1.compareto (o) // compiled successfully, but an invalidcastexception is thrown during runtime, so the type security check during compilation cannot be performed.

Somevaluetype2 v2 = new somevaluetype2 (0 );

Object o = new object ();

V2.compareto (V2); // The method declared by somevaluetype2 is called, so no packing is required.

V1.compareto (o) // compilation fails, providing type security check during compilation

}

 

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.