c# - ConcurrentDictionary Remove() 不可访问

标签 c#

所以我有这个用 .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()
    {
    }
}

现在BBar仅在访问 Bar 时可访问 作为IFoo .


1而且,事实上,在 ConcurrentDictionary<TKey,TValue>文档,即 where you'll find IDictionary.Remove listed .

关于c# - ConcurrentDictionary Remove() 不可访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51496851/

相关文章:

c# - 无法访问 .NET Standard 类库中的 HttpRuntime.Cache

c# - "()=>"的目的是什么

c# - 如何在 Asp.Net MVC Razor 中创建 'generic' 控件?

c# - 在 ASP.NET MVC 中,没有 AntiForgeryToken 的删除操作方法不安全吗?

c# - 无法多次将日期添加到循环中的日期

c# - 无法计算表达式,因为当前线程处于堆栈溢出状态

c# - ASP.NET Core CORS WebAPI : no Access-Control-Allow-Origin header

c# - 使用grpc构建asp net core,错误: wellknowntype not found

c# - 如何将数据列表从 C# 传递到 MySQL 存储过程

c# - Redis 缓存的最小起订量单元测试