How to use case-insensitive dictionary in C#

1 Answer

0 votes
using System.Collections.Generic;
using System;

namespace ConsoleApplication_C_Sharp
{
    class Program
    {
        static void Main(string[] args)
        {

            var caseInsensitiveDic = new Dictionary<string, int>(StringComparer.OrdinalIgnoreCase);

            caseInsensitiveDic.Add("c#", 1);
            caseInsensitiveDic.Add("C", 2);
            caseInsensitiveDic.Add("Java", 3);
            caseInsensitiveDic.Add("python", 4);

            Console.WriteLine(caseInsensitiveDic["JAVA"]);

            if (caseInsensitiveDic.ContainsKey("PYTHON"))
                Console.WriteLine("yes");



            var dic = new Dictionary<string, int>();

            try
            {

                dic.Add("c#", 1);
                dic.Add("C", 2);
                dic.Add("Java", 3);
                dic.Add("python", 4);

                Console.WriteLine(dic["JAVA"]);

                if (dic.ContainsKey("PYTHON"))
                    Console.WriteLine("yes");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }
}


/*
run:

3
yes
The given key was not present in the dictionary.

*/

 



answered Mar 7, 2017 by avibootz

Related questions

1 answer 132 views
2 answers 74 views
1 answer 143 views
1 answer 153 views
1 answer 161 views
2 answers 233 views
233 views asked Mar 31, 2017 by avibootz
2 answers 222 views
...