c# – 具体类如何隐藏它实现的接口的成员

我将举一个.NET的例子.
ConcurrentDictionary<TKey,TValue> : IDictionary<TKey,TValue>,IDictionary

在这里您可以看到ConcurrentDictionary实现字典接口.但是我无法访问Add< TKey,TValue> ConcurrentDictionary实例中的方法.这怎么可能?

IDictionary<int,int> dictionary = new ConcurrentDictionary<int,int>();
dictionary.Add(3,3); //no errors

ConcurrentDictionary<int,int> concurrentDictionary = new ConcurrentDictionary<int,int>();
concurrentDictionary.Add(3,3); //Cannot access private method here

更新:

我知道如何访问它,但我不知道显式实现接口可以允许将访问修饰符更改为内部.它仍然不允许将其设为私有.它是否正确?关于该部分的更详细解释将是有帮助的.另外,我想知道一些有效的用例.

解决方法

它实现为 explicit interface implementation,这意味着你需要一个IDictionary的变量< TKey,TValue>键入以访问它.

请参阅显式接口实现部分下的ConcurrentDictionary<TKey,TValue>文档.

如果将并发字典转换为IDictionary< TKey,则可以在其上调用Add.

I didn’t kNow explicitly implementing an interface Could allow changing access modifiers to internal. It still doesn’t allow making it private though. Is this correct?

不,这不正确.

显式接口实现不会更改访问修饰符.它们改变了如何访问以这种方式实现的成员的方式(即要求您使用接口类型的变量).它们仍然是公共成员,但只能使用接口类型而不是实现类型来访问.

相关文章

原文地址:http://msdn.microsoft.com/en-us/magazine/cc163...
前言 随着近些年微服务的流行,有越来越多的开发者和团队所采...
最近因为比较忙,好久没有写博客了,这篇主要给大家分享一下...
在多核CPU在今天和不久的将来,计算机将拥有更多的内核,Mic...
c语言输入成绩怎么判断等级
字符型数据在内存中的存储形式是什么