Dictionary TryGetValue case insensitive
C# Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
class Program { static void Main(string[] args) { //initialize a dictionary with keys and values. Dictionary<string, int> birds = new Dictionary<string, int>(StringComparer.OrdinalIgnoreCase) { {"Swan Goose",10}, {"Snow Goose",20}, {"Canada Goose",30}, {"Whooper Swan",40} }; Console.WriteLine("dictionary keys and values.........."); foreach (KeyValuePair<string, int> pair in birds) { Console.WriteLine(pair.Key + " ........ " + pair.Value); } int value; string key = "SNOW Goose"; Console.WriteLine("\nkey [" + key + "] value is: "); if (birds.TryGetValue(key, out value)) { Console.WriteLine(value); } else { Console.WriteLine("key [" + key + "] does not exists in dictionary"); } Console.ReadLine(); } } |
Output: