隐VS显式接口实现 [英] implicit vs explicit interface implementation

查看:175
本文介绍了隐VS显式接口实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
  <一href="http://stackoverflow.com/questions/143405/c-interfaces-implicit-and-explicit-implementation">C#:接口 - 隐显落实

会有人解释这两个野兽,以及如何使用它们之间的差异。 AFAIK,许多pre.2.0类是没有泛型类型实现的,从而使后者的版本来实现接口两种口味。是唯一的情况下,为什么人会需要使用它们?

Would someone explain the differences between these two beasts and how to use them. AFAIK, many pre.2.0 classes were implemented without generic types, thus causing latter version to implement both flavors of interfaces. Is the the only case why one would need to use them?

你能深入如何使用它们也解释了?

Can you also explain in depth how to use them.?

感谢

推荐答案

<一个href="http://blogs.msdn.com/mhop/archive/2006/12/12/implicit-and-explicit-interface-implementations.aspx">There大约是一个很好的和pretty的详细的博客文章。

基本上与隐式接口实现您访问接口的方法和属性,好像他们是类的一部分。对于显式接口实现,你只能访问后治理,当它作为接口。

Basically with implicit interface implementation you access the interface methods and properties as if they were part of the class. With explicit interface implementations you can only access then when treating it as that interface.

在时,你可以使用一个比其他的方面,有时你必须使用显式接口实现,你要么有一个属性/方法相同的签名接口或要实现两个接口具有相同的签名,并有不同的实现为匹配的属性/方法

In terms of when you would use one over the other, sometimes you have to use explicit interface implementation as you either have a property/method with same signature as the interface or you want to implement two interfaces with the same signatures and have different implementations for those properties/methods that match.

下面的规则是由布拉德·艾布拉姆斯设计指南博客

The below rules are from Brad Abrams design guidelines blog.

  • 请不要使用显式成员作为一个安全边界。他们可以通过谁投实例的接口的客户端调用。
  • 请使用明确的成员来隐藏实现细节
  • 请使用明确的成员来近似专用接口的实现。
  • 请公开访问任何子类可以覆盖显式实现成员的另一种方式。用同样的方法名称,除非会出现冲突。

它在布拉德的博客,有拳击涉及使用显式实现价值类型时,所以要知道的性能开销的评论也提到。

It's also mentioned in the comments in the Brad's blog that there is boxing involved when using explicit implementation on value types so be aware of the performance cost.

这篇关于隐VS显式接口实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆