代码之家  ›  专栏  ›  技术社区  ›  Luca

List<T>没有实现SyncRoot!

  •  12
  • Luca  · 技术社区  · 15 年前

    每个人都用很多单子。我需要遍历这个列表,所以我使用已知的 SyncRoot 模式。

    最近我注意到 this

    问题是 List<T> 类不实现SyncRoot属性,即使实现 ICollection 同步根 财产。我说这是鲍尔斯的密码

     List<int> list = new List<int>()
     list.SyncRoot;
    

    请给出以下编译器错误:

    错误CS0117:“System.Collections.Generic.List”不包含“SyncRoot”的定义

    …如果这是真的,那么在遍历List<T>类型的公共属性时,如何同步它?

    1 回复  |  直到 9 年前
        1
  •  19
  •   ChaosPandion    15 年前

    它实际上是显式实现的。

    object ICollection.SyncRoot
    {
        get
        {
            if (this._syncRoot == null)
            {
                Interlocked.CompareExchange(ref this._syncRoot, new object(), null);
            }
            return this._syncRoot;
        }
    }
    

    ICollection 使用它。