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. Python实现图片切割

    import os from PIL import Image def splitimage(src, rownum, colnum, dstpath): img = Image.open(src) ...

  2. Xcode8、 iOS10 适配问题

    调用相机.相册.麦克风.位置等隐私问题崩溃解决办法 你的项目中访问了隐私数据,比如:相机,相册,联系人等,在Xcode8中打开编译的话,统统会crash,控制台会输出下面这样的日志: 这是因为iOS对 ...

  3. xtu summer individual 5 A - To Add or Not to Add

    To Add or Not to Add Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeFo ...

  4. Leetcode 212.单词搜索II

    单词搜索II 给定一个二维网格 board 和一个字典中的单词列表 words,找出所有同时在二维网格和字典中出现的单词. 单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中"相邻&q ...

  5. 九度oj 题目1057:众数

    题目1057:众数 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:9744 解决:3263 题目描述: 输入20个数,每个数都在1-10之间,求1-10中的众数(众数就是出现次数最多的数, ...

  6. Javascript类型转换的规则全面&附有实例

    Javascript的变量是松散类型的,它可以存储Javascript支持的任何数据类型,其变量的类型可以在运行时被动态改变.请看示例: 1 2 3 var n = 10; n = "hel ...

  7. HDU 3749 Financial Crisis(点-双连通分量)

    Because of the financial crisis, a large number of enterprises go bankrupt. In addition to this, oth ...

  8. 【NOIP2015】运输计划(树上差分,二分答案)

    题意:一棵有边权的树上有m条路径,要求选择一条边使其边权变为0,使得最大路径长度最小 n,m<=300000 思路:直接求最优方案不可做,但检验对于某一个ans是否能有方案是可行的 取出所有总长 ...

  9. 到达时间自动点击按钮弹出提示并跳转【JavaScript实现】

    原文发布时间为:2008-10-11 -- 来源于本人的百度文章 [由搬家工具导入] 其实我本来是想 做 在线考试的时候 规定时间到达时候自动交卷的,就想到这个例子了。。。。 代码: <html ...

  10. struts2开发action 的三种方法以及通配符、路径匹配原则、常量

    struts2开发action 的三种方法 1.继承ActionSupport public class UserAction extends ActionSupport { // Action中业务 ...