static void Main(string[] args) { string s = "this is a test"; Dictionary<char, int> dict = GetCharCount(s); //foreach (KeyValuePair<char, int> v in dict) foreach (var v in GetCharCount(s)) { Console.WriteLine("Key:{0},Value:{1}", v.Key, v.Value); } } public static Dictionary<char, int> GetCharCount(string str) { Dictionary<char, int> dict = new Dictionary<char, int>(); foreach (char c in str) { if (dict.ContainsKey(c)) { dict[c] += 1; } else { dict[c] = 1; } } return dict; }