How to create dictionary from string array with Linq in C#

1 Answer

0 votes
using System;
using System.Linq;

namespace ConsoleApplication_C_Sharp
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] arr = new string[] { "c#",
                                          "java",
                                          "c++"
                                        };
    
            var dictionary = arr.ToDictionary(item => item, item => true);

            foreach (var keyval in dictionary)
            {
                Console.WriteLine("Key = {0} - Value = {1}", keyval.Key, keyval.Value);
            }
        }
    }
}

/*
run:

Key = c# - Value = True
Key = java - Value = True
Key = c++ - Value = True

*/

 



answered Jan 4, 2017 by avibootz
...