Home > Net >  Using Dictionary<TKey,TValue>(IEnumerable<KeyValuePair<TKey,TValue>>) Constructor
Using Dictionary<TKey,TValue>(IEnumerable<KeyValuePair<TKey,TValue>>) Constructor

Time:06-02

I am confused as to how to use

Dictionary<TKey,TValue>(IEnumerable<KeyValuePair<TKey,TValue>>)

constructor of Dictionary Class in C#. A working example will be very helpful.

Thanks

Satish Chandra

CodePudding user response:

With the constructor you mentioned you can pass an already existing Dictionary as parameter. e.g.:

Dictionary<int,string> firstDictionary = new Dictionary<int, string> { { 0, "0" }, { 1, "hallo" } };
Dictionary<int,string> secondDictionary;

void InitSecondDict()
{
    secondDictionary = new Dictionary<int,string>(firstDictionary);
}

What this constructor does is copying the elements from "firstDictionary" into the new Dictionary<int,string>-object "secondDictionary"

CodePudding user response:

Dictionary<string, string> openWith = new Dictionary<string, string>();

More info and examples on Micrsoft docs

  • Related