score:16

Accepted answer

You can also use the Linq Enumerable.ToDictionary, specifying the key and value as null:

var myList = new List<string> { "first", "second" };
Dictionary<string, string> dict = myList.ToDictionary(
    item => item,         // key 
    item => (string)null  // value
);

Note that you need to cast "null" to your element type, otherwise C# can't infer the type of the second lambda expression.

score:1

Just using a loop is simple enough:

foreach (var item in myList)
   myDictionary.Add(item, null);

This assumes, of course, that you have already created the Dictionary.


Related Articles