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

最新文章

  1. ASP.NET MVC5+EF6+EasyUI 后台管理系统(64)-WebApi与Unity注入
  2. 用jquery解析JSON数据的方法以及字符串转换成json的3种方法
  3. Ruby中Block, Proc, 和Lambda
  4. IIS Connection Timeout vs httpRuntime executionTimeout
  5. android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error(Sqlite code 14): Could not open database,(OS error - 13:Permission denied)
  6. USB移动硬盘WinPE启动盘的制作方法
  7. Java如何转换protobuf-net中的bcl.DateTime对象
  8. 第03章-VTK系统概述(1)
  9. 【Python】 文件目录比较工具filecmp和difflib
  10. 详解CSS选择器、优先级与匹配原理【转】
  11. Python 爬虫之下载图片
  12. js获取url协议、url, 端口号等信息路由信息
  13. ELK(elasticsearch+kibana+logstash)搜索引擎(二): elasticsearch基础教程
  14. multi_index_container 多索引容器
  15. jdk8的特性stream().map()
  16. jsonServer 造个假的服务器传递数据接口 再用axois来请求数据
  17. 分块学习笔记qwq
  18. (转)Elasticsearch查询规则------match和term
  19. Vibrator震动
  20. Windows系统优化

热门文章

  1. PHP:POST OR GET 请求
  2. linux下mysql的安装与使用
  3. Spider-Python爬虫之PyQuery基本用法
  4. MyBatis 多参问题
  5. Spring Boot 2 (三):Spring Boot 开源软件都有哪些?
  6. python基础示例
  7. AspNetPager控件的简单使用
  8. xtu read problem training 4 B - Multiplication Puzzle
  9. 《C语言程序设计(第四版)》阅读心得(四 文件操作)
  10. android studio配置so文件路径