Counting the number of character occurrences I've written some code that uses a SortedDictionary to count the number of occurrences of a character in a string.

How can I improve this code? What should I be doing differently? Any advice at all would be helpful.

    static void Main(string[] args)
    {
        string longText = @"The quick brown fox jumps over the lazy dog";

        var count = CharacterCount.Count(longText);

        foreach (var character in count)
        {
            Console.WriteLine("{0} - {1}", character.Key, character.Value);
        }
    }


CharacterCount class:

class CharacterCount
{
    public static SortedDictionary&lt;char, ulong&gt; Count(string stringToCount)
    {
        SortedDictionary&lt;char, ulong&gt; characterCount = new SortedDictionary&lt;char, ulong&gt;();

        foreach (var character in stringToCount)
        {
            if (!characterCount.ContainsKey(character))
            {
                characterCount.Add(character, 1);
            }
            else
            {
                characterCount[character]++;
            }
        }

        return characterCount;
    }


Here is the output:
You could do the same thing with LINQ:

var counts = longText.GroupBy(c =&gt; c) // put each character into a "bucket"
                     // order the buckets alphabetically
                     .OrderBy(c =&gt; c.Key);
                     // then convert to dictionary where key = character, value = count
                     .ToDictionary(grp =&gt; grp.Key, grp =&gt; grp.Count()) 


For convenience/readability, you could turn this into an extension method on string:

static class Extensions 
{
    public static Dictionary&lt;char, int&gt; CharacterCount(this string text)
    {
        return text.GroupBy(c =&gt; c)
                   .OrderBy(c =&gt; c.Key)
                   .ToDictionary(grp =&gt; grp.Key, grp =&gt; grp.Count());
    }
}


And invoke it like this:

var longText = @"The quick brown fox jumps over the lazy dog";

var counts = longText.CharacterCount();


However, note that the above doesn't produce a sorted dictionary in the sense that if you were to add another key/value to it afterwards, the order would no longer be maintained. You could re-sort at that point, or use this SortedDictionary constructor to create a sorted dictionary.