How do you convert a dictionary to a ConcurrentDictionary?

后端 未结 4 1085
眼角桃花
眼角桃花 2021-01-17 12:04

I have seen how to convert a ConcurrentDictionary to a Dictionary, but I have a dictionary and would like to convert to a ConcurrentDictionary. How do I do that?... better

4条回答
  •  庸人自扰
    2021-01-17 12:40

    Use ConcurrentDictionary Constructor (IEnumerable>) constructor which can accept a dictionary object like:

    Dictionary dictionary = new Dictionary();
    dictionary.Add(1,"A");
    dictionary.Add(2, "B");
    
    ConcurrentDictionary concurrentDictionary = 
                 new ConcurrentDictionary(dictionary);
    

    can i set the LINQ statement to be a ConcurrentDictionary?

    No. You can not.. There is no extension method available to create ConcurrentDictionary in LINQ. You can either create your own extension method or you can use the ConcurrentDictionary constructor in your LINQ query while projecting results.

提交回复
热议问题