Currently I was working on parallel threads in C#. So, I have confusion of using ConcurrentBag<T>
and List<T>
.
Here is my code:
public async Task<ConcurrentDictionary<string, ConcurrentBag<T>>> MethodA(SearchResults<Result> response)
{
var a = new ConcurrentDictionary<string, ConcurrentBag<DeviceAlertAlarm>>();
var tasks = response.GetResults().Select(async result) =>
{
var b = new List <T>();
// do something
a["xxx"] = b;
});
await Task.WhenAll(tasks);
return a;
}
For var b = new List ();
Is it mandatory of ConcurrentBag<T>
in multi-threading or can I use List<T>
which is best way of writing the code with respective of performance.
Which one is better Concurrentbag<T>
or List<T>
in the above part of code?