ConcurrentBag - Add Multiple Items?

后端 未结 5 1891
庸人自扰
庸人自扰 2021-01-01 08:21

Is there a way to add multiple items to ConcurrentBag all at once, instead of one at a time? I don\'t see an AddRange() method on ConcurrentBag, but there is a Concat(). How

5条回答
  •  我在风中等你
    2021-01-01 09:13

    Yes :)

    Concat is perhaps one of the Enumerable extensions. It doesn't add anything to the ConcurrentBag, it just returns some funky object containing the original bag and whatever you tried to add there.

    Beware that the result of Concat is not a ConcurrentBag anymore, so you would not want to use it. It's a part of general LINQ framework, making possible to combine immutable sequences. This framework, of course, doesn't try to extend the concurrent properties of the operands to the result, so the resulting object will not be so well suited for multithreaded access.

    (Basically, Concat applies to ConcurrentBag because it exposes IEnumerable interface.)

提交回复
热议问题