Is this the proper way to iterate over Concurrentdictionary in C#

前端 未结 2 670
野的像风
野的像风 2021-01-07 16:28

I\'m only using this code for an example. Assume I have the following Person class.

using System;
using System.Collections.Generic;
using System.Linq;
using          


        
2条回答
  •  醉话见心
    2021-01-07 16:46

    Take a look at this article.

    TryRemove() was added to attempt atomic, safe removes.
    
        To safely attempt to remove a value we need to see if the key exists first, this checks for existence and removes under an atomic lock.
    

    Since TryRemove will remove the item from collection, you might need the value of the key.

    It is safe to iterate it with foreach. You wont get an exception.

提交回复
热议问题