How to remove a single, specific object from a ConcurrentBag<>?

后端 未结 9 528
误落风尘
误落风尘 2021-02-02 04:34

With the new ConcurrentBag in .NET 4, how do you remove a certain, specific object from it when only TryTake() and TryPeek() are

9条回答
  •  佛祖请我去吃肉
    2021-02-02 05:24

    public static void Remove(this ConcurrentBag bag, T item)
    {
        while (bag.Count > 0)
        {
            T result;
            bag.TryTake(out result);
    
            if (result.Equals(item))
            {
                break; 
            }
    
            bag.Add(result);
        }
    
    }
    

提交回复
热议问题