Is the List.AddRange() thread safe?

前端 未结 5 1708
天命终不由人
天命终不由人 2021-02-07 09:14

Can I, without locking, safely call List.AddRange(r) from multiple threads? If not, what sort of trouble would I run into?

5条回答
  •  [愿得一人]
    2021-02-07 09:54

    Depending on your usage, SynchronizedCollection could work.

    You'd have no single-shot AddRange though. If you are only using this to seed the collection, you can do this as there is an IEnumerable constructor overload.

提交回复
热议问题