相关疑难解决方法(0)

隐式vs显式接口实现

可能重复:
C#:接口 - 隐式和显式实现

有人会解释这两种野兽之间的差异以及如何使用它们.AFAIK,许多pre.2.0类在没有泛型类型的情况下实现,因此导致后一版本实现这两种接口.是否需要使用它们的唯一情况?

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

谢谢

.net c# interface

73
推荐指数
2
解决办法
4万
查看次数

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

我将举一个.NET的例子.

ConcurrentDictionary<TKey, TValue> : IDictionary<TKey, TValue>, IDictionary
Run Code Online (Sandbox Code Playgroud)

在这里您可以看到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
Run Code Online (Sandbox Code Playgroud)

更新:

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

.net c#

6
推荐指数
3
解决办法
593
查看次数

标签 统计

.net ×2

c# ×2

interface ×1