Skip to content

Instantly share code, notes, and snippets.

@karenpayneoregon
Created May 21, 2024 12:43
Show Gist options
  • Save karenpayneoregon/c229ce3da029a89079807d551b235ca4 to your computer and use it in GitHub Desktop.
Save karenpayneoregon/c229ce3da029a89079807d551b235ca4 to your computer and use it in GitHub Desktop.
Case insensitive string key dictionary
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