简体   繁体   English

c# 中的显式和隐式接口实现有什么区别

[英]what is the difference between explicit and implicit interface implementation in c#

what is the difference between explicit interface and implicit interface implementation in c#/asp.net? c#/asp.net中的显式接口和隐式接口实现有什么区别? in which scenario we can use the explicit interface and implicit interface implementation.在哪种情况下我们可以使用显式接口和隐式接口实现。

Thanks,谢谢,

Pradeep普拉迪普

The concept behind implicit and explicit implementation is rather simple:隐式和显式实现背后的概念相当简单:

  • A member with implicit implementation will be accessible through the interface as well as through the class implementing it可以通过接口以及实现它的 class 访问具有隐式实现的成员
  • A member with explicit implementation will be accessible through the interface only具有显式实现的成员只能通过接口访问

As for why and when you would use one or another, it depends.至于为什么以及何时使用一种或另一种,这取决于。 In the case you're implementing more than one interface with the same property/method, explicit implementation is your only option, as it is the only way to know which property/method you're intending to call.如果您使用相同的属性/方法实现多个接口,则显式实现是您唯一的选择,因为它是了解您打算调用哪个属性/方法的唯一方法。 Obviously in this scenario you can't have that property/method on the class itself: if there is, it will be class only, and will not match any of the interfaces (which will have their explicit implementation of it).显然,在这种情况下,您不能在 class 本身上拥有该属性/方法:如果有,它将仅是 class,并且不会匹配任何接口(将有其显式实现)。

In other scenarios, it really depends on your design choices and what you're trying to accomplish.在其他情况下,这实际上取决于您的设计选择以及您要完成的工作。 If you want to force the caller to access interface members only through interface, and not through class declaration, do an explicit implementation.如果要强制调用者仅通过接口访问接口成员,而不是通过 class 声明,请执行显式实现。

Say you have two interfaces, IDoStuff<T> and IDoStuff , which your class implements.假设您有两个接口IDoStuff<T>IDoStuff ,您的 class 实现了它们。 They both have a method "GetStuff", but one has the signature T GetStuff() , and the other has the signature object GetStuff() .它们都有一个方法“GetStuff”,但一个具有签名T GetStuff() ,另一个具有签名object GetStuff()

The problem is that .net will not let you have two methods named the same thing that only differ on the return type.问题是 .net 不会让您拥有两个名称相同但返回类型不同的方法。 But you need to have both of these methods in your class to satisfy both interfaces.但是您需要在 class 中同时拥有这两种方法才能满足这两个接口。 If T is, in fact, an object , then you can use explicit implementation like so.如果T实际上是一个object ,那么您可以像这样使用显式实现。

public T GetStuff()
{
  T stuff;
  //Stuff Is Got
  return stuff;
}

IDoStuff.GetStuff()
{
  return (object)GetStuff();
}

Note that because IDoStuff mandates the security requirements of GetStuff , IDoStuff.GetStuff will be public/private/protected/internal based on that interface's declaration.请注意,由于IDoStuff强制要求GetStuff的安全要求,因此IDoStuff.GetStuff将基于该接口的声明是public/private/protected/internal的。

If you wanted, you could do every implantation explicitly, but the full method name for each would be InterfaceName.MethodName , and that gets a little annoying to read and write.如果你愿意,你可以显式地进行每一次植入,但是每个方法的完整方法名都是InterfaceName.MethodName ,读和写起来有点烦人。 Usually this is only used when you want to implement a method with the same signature multiple times to satisfy several interfaces.通常这仅在您想多次实现具有相同签名的方法以满足多个接口时使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM