C# 隐式与显式接口实现
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/598714/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
implicit vs explicit interface implementation
提问by
Possible Duplicate:
C#: Interfaces - Implicit and Explicit implementation
可能的重复:
C#:接口 - 隐式和显式实现
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?
有人会解释这两种野兽之间的区别以及如何使用它们。AFAIK,许多 pre.2.0 类是在没有泛型类型的情况下实现的,因此导致后一个版本实现了两种风格的接口。是唯一需要使用它们的情况吗?
Can you also explain in depth how to use them.?
你也可以深入解释一下如何使用它们。?
Thanks
谢谢
采纳答案by Andrew Barrett
There is a good and pretty detailed blog post about this.
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 them 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.
以下规则来自 Brad Abrams设计指南博客。
- Do notuse explicit members as a security boundary. They can be called by any client who cast an instance to the interface.
- Douse explicit members to hide implementation details
- Douse explicit members to approximate private interface implementations.
- Doexpose an alternative way to access any explicitly implemented members that subclasses are allowed to override. Use the same method name unless a conflict would arise.
- 不要使用显式成员作为安全边界。任何将实例转换为接口的客户端都可以调用它们。
- 待办事项使用明确的成员隐藏实现细节
- 待办事项使用明确的成员大概私有接口实现。
- 一定要公开一种替代方法来访问允许子类覆盖的任何显式实现的成员。除非出现冲突,否则请使用相同的方法名称。
It's also mentioned in the comments in Brad's blog that there is boxing involved when using explicit implementation on value types so be aware of the performance cost.
Brad 博客的评论中还提到,在值类型上使用显式实现时会涉及装箱,因此请注意性能成本。
回答by Tanveer Badar
There is one more way to look at it, from the labyrinthine implementation itself, here: http://blogs.msdn.com/cbrumme/archive/2003/05/03/51381.aspx.
还有另一种看待它的方法,从迷宫实现本身,这里:http: //blogs.msdn.com/cbrumme/archive/2003/05/03/51381.aspx。
But in short, implicit implementation gives you an is-a type conversion, explicit implementation won't be accessible unless the object is explicitly type cast to that interface type.
但简而言之,隐式实现为您提供了 is-a 类型转换,除非对象显式转换为该接口类型,否则无法访问显式实现。
回答by Suresh
In layman's terms, if a class inherits from 2 or more interfaces and if the interfaces happen to have the same method names, the class doesn't know which interface method is being implemented if you use implicit interface implementation. This is one of the scenarios when you would explicitly implement an interface.
通俗地说,如果一个类继承自 2 个或多个接口,并且如果这些接口碰巧具有相同的方法名称,那么如果您使用隐式接口实现,则该类不知道正在实现哪个接口方法。这是显式实现接口的场景之一。
Implicit Interface Implementtation
隐式接口实现
public class MyClass : InterfaceOne, InterfaceTwo
{
public void InterfaceMethod()
{
Console.WriteLine("Which interface method is this?");
}
}
interface InterfaceOne
{
void InterfaceMethod();
}
interface InterfaceTwo
{
void InterfaceMethod();
}
Explicit Interface Implementation
显式接口实现
public class MyClass : InterfaceOne, InterfaceTwo
{
void InterfaceOne.InterfaceMethod()
{
Console.WriteLine("Which interface method is this?");
}
void InterfaceTwo.InterfaceMethod()
{
Console.WriteLine("Which interface method is this?");
}
}
interface InterfaceOne
{
void InterfaceMethod();
}
interface InterfaceTwo
{
void InterfaceMethod();
}
The following link has an excellent video explaining this concept
Explicit Interface Implementation
下面的链接有一个很好的视频解释这个概念
显式接口实现