Hi all i have a small function that is storing characters in a string into a dictionary. The string could contain lower case and upper case letters simultaneously and I wanted to store all the characters in either lower case or upper case. Basically i want the dictionary to treat 'T' and 't' as same key. Below is my code.
public bool CheckCharOddCount(string str1)
bool isOdd = false;
Dictionary<char, int> dt = new Dictionary<char, int>();
// Dictionary is case sensitive so 'T' and 't' are treated as different keys.
str1 = str1.ToLower(); # One way
foreach (char c in str1)
char.ToLower(c); # Another way
foreach (var item in dt)
if (item.Value % 2 == 1)
isOdd = true;
To create a case insensitive key dictionary, use the appropriate constructor:
Dictionary<string, int> dictionary = new Dictionary<string, int>( StringComparer.CurrentCultureIgnoreCase);