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. python3.x Day5 socket编程

    socket编程: socket 是大多应用层的底层的封装,实际封装的就是 发送,接收,但其实很复杂,在传输层协议之上(TCP/IP,UDP) 既然是网络通讯,一般按照服务端,客户端来处理:服务端: ...

  2. error trying to exec 'cc1plus': execvp: 没有那个文件或目录

    出现这个问题,有两种可能: 第一,你没有安装g++ 第二,你的gcc的版本和g++版本不相符合 安装gcc和g++及一些依赖包 sudo apt-get install build-essential ...

  3. 从yii2框架中的di容器源码中了解反射的作用

    反射简介 参考官方简介的话,PHP 5 具有完整的反射 API,添加了对类.接口.函数.方法和扩展进行反向工程的能力. 此外,反射 API 提供了方法来取出函数.类和方法中的文档注释. YII2框架中 ...

  4. 用element-ui的走马灯carousel轻松实现自适应全屏banner图

    写在前面:网站轮播图建议使用swiper组件,非常方便快捷.vue轮播图插件之vue-awesome-swiper 接手一个项目,轮播图是用element-ui的carousel实现的,看起来效果还不 ...

  5. jQuery的ready与js的load事件的区别

    摘自:http://www.cnblogs.com/see7di/archive/2011/07/15/2239677.html 为了理解这两个事件的异同,读者应该先了解HTML文档加载的顺序. DO ...

  6. 10 行 Python 代码,批量压缩图片 500 张,简直太强大了

    本文原创并首发于公众号[Python猫],未经授权,请勿转载. 原文地址:https://mp.weixin.qq.com/s/5hpFDgjCpfb0O1Jg-ycACw 熟悉 "Pyth ...

  7. Python学习之单继承与多继承

    继承 面向对象编程语言的一个主要功能就是“继承”. 继承是指这样一种能力:它可以使用现有类的所有功能,并在无需重新编写原来的类的情况下对这些功能进行扩展. (1) 单继承:python同时支持类的继承 ...

  8. selenium--driver.switchTo()-----转

    https://www.cnblogs.com/clairejing/p/9499223.html 在自动化测试中,会遇到多窗口.多iframe.多alert的情况.此时,会使用driver.swit ...

  9. prometheus监控linux系统

    安装node exporter 创建Systemd服务 #vim /etc/systemd/system/node_exporter.service[Unit]Description=mysql_ex ...

  10. baidu 和 es 使用

    http://www.cnblogs.com/kangoroo/p/8047586.html