所以我有这个用 .NET 4.7 编译的最小示例:
class Program
{
static void Main(string[] args)
{
IDictionary<int, string> dic = new ConcurrentDictionary<int, string>();
ConcurrentDictionary<int, string> sameDic = (ConcurrentDictionary<int, string>) dic;
dic.Add(1, "Hold");
dic.Add(2, "position");
dic.Remove(1); // OK
sameDic.Remove(2); // Not even compiling !!
}
}
- 然后调用
dic.Remove(1)
安全吗? - 在知道编译器不接受以下代码的情况下如何重现相同的行为?
代码:
public interface IFoo
{
void B();
}
public class Bar : IFoo
{
private void B() // Not allowed
{
}
}
最佳答案
您似乎在寻找explicit interface implementation 1。你可以这样写:
public interface IFoo
{
void B();
}
public class Bar : IFoo
{
void IFoo.B()
{
}
}
现在B
在 Bar
仅在访问 Bar
时可访问 作为IFoo
.
1而且,事实上,在 ConcurrentDictionary<TKey,TValue>
文档,即 where you'll find IDictionary.Remove
listed .
关于c# - ConcurrentDictionary Remove() 不可访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51496851/