Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert concurrentbag to list?

Tags:

c#

.net

I have few task which I can do in parallel. As list is not thread safe i am using concurrentbag. Once all the task are completed I want to convert the concurrentbag to list.

I have searched in MSDN but couldn't find any API which can convert concurrentbag to list in c#. How can I do it?

Note: i have to convert to list, It is necessary.

I have other option to apply lock on list but i wanted to use in build concurrentbag which is thread safe.

like image 363
Suri Avatar asked Jul 22 '16 06:07

Suri


2 Answers

You could use ToList extension method.

var list = concurrentBag.ToList();
like image 110
Hari Prasad Avatar answered Sep 18 '22 09:09

Hari Prasad


ConcurrentBag has the extension method of .ToList() - it implementsIEnumerable<T>

var someList = someConcurrentBag.ToList();
like image 28
Gilad Green Avatar answered Sep 20 '22 09:09

Gilad Green