https://msdn.microsoft.com/en-us/library/ms173157.aspx

If a class implements two interfaces that contain a member with the same signature,

then implementing that member on the class will cause both interfaces to use that member as their implementation.

In the following example, all the calls to Paint invoke the same method.

class Test
{
static void Main()
{
SampleClass sc = new SampleClass();
IControl ctrl = (IControl)sc;
ISurface srfc = (ISurface)sc; // The following lines all call the same method.
sc.Paint();
ctrl.Paint();
srfc.Paint();
}
} interface IControl
{
void Paint();
}
interface ISurface
{
void Paint();
}
class SampleClass : IControl, ISurface
{
// Both ISurface.Paint and IControl.Paint call this method.
public void Paint()
{
Console.WriteLine("Paint method in SampleClass");
}
}

If the two interface members do not perform the same function, however, this can lead to an incorrect implementation of one or both of the interfaces.

It is possible to implement an interface member explicitly—creating a class member that is only called through the interface, and is specific to that interface.

This is accomplished by naming the class member with the name of the interface and a period.

For example:

public class SampleClass : IControl, ISurface
{
void IControl.Paint()
{
System.Console.WriteLine("IControl.Paint");
}
void ISurface.Paint()
{
System.Console.WriteLine("ISurface.Paint");
}
}

The class member IControl.Paint is only available through the IControl interface, and ISurface.Paint is only available through ISurface.

Both method implementations are separate, and neither is available directly on the class.

For example:

// Call the Paint methods from Main.

SampleClass obj = new SampleClass();
//obj.Paint(); // Compiler error. IControl c = (IControl)obj;
c.Paint(); // Calls IControl.Paint on SampleClass. ISurface s = (ISurface)obj;
s.Paint(); // Calls ISurface.Paint on SampleClass. // Output:
// IControl.Paint
// ISurface.Paint

Explicit implementation is also used to resolve cases

where two interfaces each declare different members of the same name such as a property and a method:  //2个接口,使用同一个名称分别声明了一个属性和一个方法

interface ILeft
{
int P { get;}
}
interface IRight
{
int P();
}

To implement both interfaces, a class has to use explicit implementation either for the property P, or the method P, or both, to avoid a compiler error.

For example:

class Middle : ILeft, IRight
{
public int P() { return ; }
int ILeft.P { get { return ; } }
}

https://www.codeproject.com/Articles/1000374/Explicit-Interface-VS-Implicit-Interface-in-Csharp

Explicit Interface Implementation (C# Programming Guide)的更多相关文章

  1. DbContext 中的 Explicit interface implementation

    疑惑 前段时间一直再用Entity Framework 6,写了一些公用的方法,在这个过程中发现了DbContext实现的接口IObjectContextAdapter,可以通过这个接口访问到更底层的 ...

  2. C# explicit interface implementation(显式接口实现)

    C# explicit interface implementation 某个类要实现两个包含相同方法名的接口, 应该如何实现这两个方法? namespace ExplicitInterfaceImp ...

  3. Interfaces (C# Programming Guide)

    https://msdn.microsoft.com/en-us/library/ms173156.aspx An interface contains definitions for a group ...

  4. 【IOS笔记】View Programming Guide for iOS -1

    原文:View Programming Guide for iOS View and Window Architecture Views and windows present your applic ...

  5. [IoLanguage]Io Programming Guide[转]

    Io Programming Guide     Introduction Perspective Getting Started Downloading Installing Binaries Ru ...

  6. View Programming Guide for iOS_读书笔记[正在更新……]

    原文:View Programming Guide for iOS 1 Introduction 先熟悉一下基本概念. Window Windows do not have any visible c ...

  7. View Programming Guide for iOS ---- iOS 视图编程指南(二)---View and Window Architecture

    View and Window Architecture 视图和窗口架构 Views and windows present your application’s user interface and ...

  8. Collection View Programming Guide for iOS---(七)---Custom Layouts: A Worked Example

    Custom Layouts: A Worked Example Creating a custom collection view layout is simple with straightfor ...

  9. View Controller Programming Guide for iOS---(七)---Resizing the View Controller’s Views

    Resizing the View Controller’s Views A view controller owns its own view and manages the view’s cont ...

随机推荐

  1. c++基础_杨辉三角形

    #include <iostream> using namespace std; int main(){ int n; cin>>n; ][]; ;i<n;i++){ a ...

  2. 拥抱变革(More Fearless Change)

    今天从大敏捷群中了解到Tid2017的一个讲演,一位敏捷教练,组织变革的专著的作者Linda的讲演. <More Fearless Change-Strategy for Making Your ...

  3. 关于No Spring WebApplicationInitializer types detected on classpath的提示,tomcat 卡主

    No Spring WebApplicationInitializer types detected on classpath 下一句:Initializing Spring root WebAppl ...

  4. hihoCoder #1162 : 骨牌覆盖问题·三

    #1162 : 骨牌覆盖问题·三 Time Limit:10000ms Case Time Limit:1000ms Memory Limit:256MB 描述 前两周里,我们讲解了2xN,3xN骨牌 ...

  5. Spring & Java

    Spring & Java https://spring.io/ Spring Boot https://www.shiyanlou.com/courses/1152 Spring Boot入 ...

  6. IE & table & border & border-collapse & bug

    shit IE table border bug & border-collapse bug > `border-collapse: collapse;` table { width: ...

  7. js重新讲解继承,es5的一些继承,es6继承的改变 ----------由浅入深

    es5 利用原型公有私有继承 function Parent(name) { this.name = name } Parent.prototype.home = '北京'; function Chi ...

  8. CF676E:The Last Fight Between Human and AI

    人类和电脑在一个多项式上进行博弈,多项式的最高次项已知,一开始系数都不确定.电脑先开始操作,每次操作可以确定某次项的系数,这个系数可以是任意实数.给出一个博弈中间状态,最后如果这个多项式被x-K整除就 ...

  9. 一个1x1px大小Data/Base64数据的gif透明图片

    <img src="data:image/gif;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVQImWNg ...

  10. winform总结6=>线程和委托的关系

    基础类: using System; using System.Collections.Generic; using System.Linq; using System.Text; using Sys ...