Created
May 21, 2024 12:43
-
-
Save karenpayneoregon/c229ce3da029a89079807d551b235ca4 to your computer and use it in GitHub Desktop.
Case insensitive string key dictionary
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static TextInfo textInfo = new CultureInfo("en-US", false).TextInfo; | |
static List<string> names = ["KaRen", "YelEna", "BiLL"]; | |
private static void CaseInsensitiveDictionary1() | |
{ | |
Dictionary<string, string> userDictionary = new(StringComparer.InvariantCultureIgnoreCase) | |
{ { "karen", "Developer advocate" }, { "yelena", "Manager" }, { "bill", "Web master" }, }; | |
foreach (var name in names) | |
{ | |
Debug.WriteLine(userDictionary.TryGetValue(name, out var title) ? | |
$"{name,-10}{textInfo.ToTitleCase(name),-10}{title}" : | |
$"{name} not found"); | |
} | |
} | |
private static void CaseInsensitiveDictionary2() | |
{ | |
StringDictionary userDictionary = new() | |
{ { "KaRen", "Developer advocate" }, { "yelEna", "Manager" }, { "biLL", "Web master" } }; | |
foreach (var name in names) | |
{ | |
Debug.WriteLine(userDictionary.ContainsKey(name) | |
? $"{name,-10}{textInfo.ToTitleCase(name),-10}{userDictionary[name]}" | |
: $"{name} not found"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment