C # Interface

Source: Internet
Author: User
Class and interface implementation

Interface Definition: specify a name for a group of method signatures.

Class implementation interface, you must provide the implementation of all methods of the interface.

Even the abstract class must be fully implemented, but it can declare the interface method as abstract, and leave this interface method to the derived class for implementation, as shown below:

Public interface itest
{
Void test ();
}

Public abstract class abstractclass: itest
{
Public abstract void test ();
}

Public class concreateclass: abstractclass
{
Public override void test ()
{
// Coding implementation;
}
}

14.2 define an Interface

The interface can have methods, events, and attributes, because the latter is essentially a method.

The interface cannot have static members (including constants/enumerations)

Interfaces can be "inherited" or considered as conventions that contain another interface, rather than inheritance of the true meaning.

All members under the interface. The default value is public. Do not declare

14.3 implementation Interface

The implemented interface method must be marked as public. In this case, it is virtual and sealed in Il, that is, the subclass is not allowed to override this method (new does not work at this time ).

To mark the called interface method as virtual, You can override this method in the subclass.

// If the display is marked as sealed, it cannot be rewritten.

14.4 call the Interface Method

At runtime, you can transform a variable from one interface type to another interface type, as long as the object type implements these two interfaces, as follows:

 

// String implements icloneable and ienumerable
String S = "Jax ";
Icloneable cloneable = s;
Ienumerable enumable = (ienumerable) cloneable;

Note: cloneable can only use the icloneable interface method, but cannot use the string method. Although the enumable variable is transformed from icloneable, it cannot use the icloneable interface method.

The value type can also be used as an interface, but it must be packed before it is converted to an interface type-the interface variable must be a reference to an object on the stack.

// Int32 implements the iformattable Interface
Int32 I = 0;
// Pack I before converting it into an interface type
Iformattable formattable = (iformattable) I;

14.5 implicit/display implementation of interface methods

Interface methods are generally implemented implicitly, and the accessibility must be declared as public.

Eimi: display the interface method implementation. Use the name of the interface that defines the method as the method name prefix.

It is not part of a type object. It only connects an interface to the type and avoids exposing behaviors and methods.

You cannot specify the accessibility public/private -- mark it as private in Il. You can only access this method through interface variables to prevent direct access to type objects.

It cannot be marked as virtual or overwritten.

Public interface itest
{
Void test ();
}

Public class testclass: itest
{
Public void test ()
{
Console. writeline ("1 ");
}

Void itest. Test ()
{
Console. writeline ("2 ");
}
} Testclass T = new testclass ();
T. Test (); // output 1

Itest it = (itest) T;
It. Test (); // output 2

 

14.6 generic interface

Three Benefits

1. type security during compilation

Int32 x = 1;
String S = "1 ";

Icomparable c = X;
C. compareto (X );
C. compareto (s); // runtime error because the object is of an insecure type

Icomparable <int32> cc = x; // strong type. Therefore, it is directly subject to integer x and does not accept string S. Otherwise, an error is returned during compilation.

2. Reduce packing when operating Value Type

In the previous example, the method for uploading X to compare is to be packed. The generic type is not used for packing and is passed by value.

Non-generic types still exist in FCL for backward compatibility.

 

3. The same class can implement the same generic interface several times, as long as different types of parameters are used

Public sealed class number: icomparable <int32>, icomparable <string>
{
Public int compareto (INT other ){}

Public int compareto (string other ){}
}

Icomparable <int32> and icomparable <string> can be considered as two different interfaces.

14.7 parameter constraints for generic Interfaces

Two benefits:

1. type parameters can be restricted to multiple interfaces, so that all interface constraints must be implemented for the input parameter types

2. Reduce packing

Parameter constraints generate a specific il language so that the interface method can be called directly on the value type without packing.

14.9 use eimi to improve the type security during compilation

Use eimi technology to process non-generic interfaces to ensure type security

 

Struct somevaluetype: icomparable
{
Private int32 m_x;

Public somevaluetype (int32 X)
{
M_x = X;
}

// This is a type-safe method
Public int32 compareto (somevaluetype other)
{
Return m_x-other. m_x;
}

// This is a type-insecure method, but it must be used to ensure the compilation is successful. Because of the different parameters, it can be considered as overload, and this method is the implementation of the interface method.
Int32 icomparable. compareto (object other)
{
Return compareto (somevaluetype) other );
}
}

Note the following when calling:

Somevaluetype v = new somevaluetype ();
Object o = new object ();

Int32 n = V. compareto (V); // comapre method of Class Object V to ensure type security

Icomparable c = V;
N = C. compareto (v );
N = C. compareto (o); // The comapre method of the interface Object C, which does not guarantee the type security, so do not use the interface object

14.10 disadvantages of eimi

Three disadvantages:

1. I have not explained how to implement an eimi method.

 

2. When a value type instance is converted to an interface type, it will be boxed

3. The eimi method cannot be inherited by the derived type

14.11 interface and class inheritance

Class inheritance: indicates is-. Easy to use, without providing all implementations; override and new rewriting; easy to add members to the base class without modifying subclass

Interface: Can-do. None of the above class inheritance advantages.

The value type is inherited from system. valuetype and can only be used by interfaces.

FCL sets are based on interfaces because various sets are rarely shared.Code.

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.