List doesn't implements SyncRoot!

后端 未结 1 1393
小鲜肉
小鲜肉 2021-01-01 20:57

Everyone use lot of List. I need to iterate over this list, so I use the known SyncRoot pattern.

Recently I noticed in this post that the SyncRoot should be avoided

1条回答
  •  醉梦人生
    2021-01-01 21:19

    It is actually implemented explicitly.

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

    This means you must cast to ICollection to use it.

    0 讨论(0)
提交回复
热议问题