C# dictionary keys case insensitive

static void TestDictionaryInSensitive()
        {
            try
            {
                var comparer = StringComparer.OrdinalIgnoreCase;
                Dictionary<string, string> dic = new Dictionary<string, string>(comparer);
                dic.Add("Hello", "World");
                dic.Add("hello", "world");
            }
            catch(Exception ex)
            {
                Console.WriteLine(ex.Message);
            } 
        }

Run the abvoe demo,it will raise an exception with "An item with the same key has already been added." message

posted @ 2021-01-27 11:06  FredGrit  阅读(88)  评论(0编辑  收藏  举报