Friday, September 30, 2022

[FIXED] How to remove all Items from ConcurrentBag?

Issue

How to clear the ConcurrentBag? it don't have any method like Clear or RemoveAll...


Solution

Although it might not completely clear due to a potential race condition, this is sufficient:

while (!myBag.IsEmpty) 
{
   myBag.TryTake(out T _);
}


Answered By - Daniel A. White
Answer Checked By - Dawn Plyler (PHPFixing Volunteer)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.