With Xiaojing, read CLR via C # (17)-Interface

Source: Internet
Author: User

CLR cannot inherit multiple base classes, but can inherit multiple interfaces. Any instance that can use a named interface can use an instance of the interface type. Interfaces provide uniform names for a group of method signatures, but do not provide any implementation. Specific classes must be implemented for all inherited interfaces.

1. Define the interface

Interface is usedInterfaceThe keyword defines the signature for a group of methods. The interface name generally starts with the letter I. You can also define events, indexers, and attributes for the interface, but do not define constructors and instance fields, you cannot construct any static members. For example:

Public interface ishout
{
Public ishout (); // × error interfaces cannot contain Constructors
Static void shout1 (); // × error the modifier 'static 'is not valid for this item
Void shout ();
String name {Get; set ;}
}

2. interface methods

After a specific class inherits an interface, you must provide implementation for the methods defined in the interface. First, we define an interface and then define a class to implement this interface.

Public interface iIntroduce
{
Void shout ();
Void description ();
}

The interface method, that is, the method defined in the Implementation interface in a specific class. The implementation of this method should pay attention to the following points:

    • The interface method must be public;
    • CLR requires that interface methods be marked as virtual; otherwise, the compiler automatically marks virtual and sealed.

Public class animal: iIntroduce
{
Public void shout () // The interface method is marked as virtual
{
Console. writeline ("Animal shout .");
}
Public Virtual void description () // mark the interface method as virtual
{
Console. writeline ("Animal description .");
}
}

View IlCode:

    • The derived class can use the new keyword to provide its own implementation for the interface.

Public class Dog: Animal
{
Public override void description () // rewrite the interface method of the base class
{
Console. writeline ("Dog description! ");
}
}

Public class Cat: Animal, iIntroduce
{
Public override void description () // rewrite the interface method of the base class
{
Console. writeline ("cat description! ");
}
New public void shout () // re-implement the Interface Method
{
Console. writeline ("cat shout .");
}
}

3. Explicit interface implementation

There are two methods to implement the interface: implicit implementation and explicit implementation. When multiple interfaces contain methods with the same name and signature, use the display interface method.

Public interface idemo1
{
Void func ();
}
Public interface idemo2
{
Void func ();
}
Public class Demo: idemo1, idemo2
{
Public void func ()
{
Console. writeline ("demo. func ()");
}
Void idemo1.func ()
{
Console. writeline ("idemo1.func ()");
}
Void idemo2.func ()
{
Console. writeline ("idemo2.func ()");
}
}

Class Program
{
Static void main (string [] ARGs)
{
Demo demo = new demo ();
Demo. func (); // call the public method func () in the demo class ()
(Idemo1) demo). func (); // explicitly call func () in idemo1 ()
(Idemo2) demo). func (); // explicitly call func () in idemo2 ()
Console. Read ();
}
}

Call result:

Note the following when implementing an explicit interface:

    • Access, such as public, is not allowed. When you view metadata, it is automatically marked as private.
    • Cannot be marked as virtual.
    • Explicit interfaces should be used with caution, because value-type instances are boxed when transformed to interfaces, and explicit interface methods cannot be inherited by Derived classes.
4. generic interface

FCL provides many ready-made interfaces such as icomparable and its generic interface form icomparable <t>. in this way, the types can be checked during compilation, which improves the type security and reduces the binning operation for converting parameters to object types, thus improving the performance.

Int x = 1;

Icomparable y = "2 ";

Y. compareto (x); // The code is compiled and the runtime is incorrect.
Icomparable <int> Z = 3;
Z. compareto (x); // compiled and run

5. base class vs Interface
    • If a IS-A link uses a base class, a can-do link exists, using interfaces.
    • It is easier to implement the base class. The function derived class provided by the base class can be slightly modified, while the interface method must implement all the members.
    • Version Control: After adding a new method to the base class, the derived class can be directly used. After adding a new method to the interface, you need to modify it.Source codeAnd re-compile.

 

 

You may like:Reading CLR via C # (00) with Xiaojing-opening part and Directory

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.