Explicit interface implementation (C # programming guide)

Source: Internet
Author: User

If the class implements two interfaces and these two interfaces contain members with the same signature, implementing this member in the class will result in both interfaces using this member as their implementation. For example:

Interface iControl
{
Void paint ();
}
Interface isurface
{
Void paint ();
}
Class sampleclass: iControl, isurface
{
// Both isurface. Paint and iControl. Paint call this method.
Public void paint ()
{
}
}

However, if two interface members execute different functions, this may result in incorrect implementation of one of the interfaces or incorrect implementation of both interfaces. You can explicitly implement interface Members-that is, create a class member that is called only through this interface and is specific to this interface. This is implemented by using the interface name and a period to name the class members. For example:

Public class sampleclass: iControl, isurface
{
Void iControl. Paint ()
{
System. Console. writeline ("iControl. Paint ");
}
Void isurface. Paint ()
{
System. Console. writeline ("isurface. Paint ");
}
}

Class member iControl. Paint can only be used through the iControl interface, and isurface. Paint can only be used through isurface. Both methods are separated and cannot be directly used in the class. For example:

Sampleclass OBJ = new sampleclass ();
// Obj. Paint (); // compiler error.

IControl c = (iControl) OBJ;
C. Paint (); // callicontrol. Paint on sampleclass.

Isurface S = (isurface) OBJ;
S. Paint (); // callisurface. Paint on sampleclass.

Explicit implementation is also used to resolve the situation where two interfaces declare different members with the same name (such as attributes and methods:

Interface ileft
{
Int P {Get ;}
}
Interface iright
{
Int P ();
}

To implement both interfaces at the same time, the class must use explicit implementation for Attribute P and/or Method P to avoid compiler errors. For example:

Class middle: ileft, iright
{
Public int P () {return 0 ;}
Int ileft. P {get {return 0 ;}}
}

 

 

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.