There is a list and a dictionary of the form.

List<string> from = new List<string>( new string[] { "1", "2", "3" }); Dictionary<string, object> to = new Dictionary<string, object>(); 

For each element of the list that is used as a key, an object is created and placed in the dictionary.

 foreach (var f in from) { to.Add(f, new Object()); } 

Is it possible to shorten the code so that the initialization of the dictionary takes place in a single line (after declaring a variable)?

    1 answer 1

     var dict = new List<string>( new string[] { "1", "2", "3" }).ToDictionary(el=>el, el=>new Object()); 

    C # ToDictionary Method