C# Practice Exercises

Word Frequency Dictionary

Count normalized words with Dictionary<TKey,TValue>.

Introduction

This exercise practices keyed lookup, normalization, and collection expressions.

GetValueOrDefault provides zero for unseen words before the count is incremented.

Runnable C# example

C#
Run code →
Program.cs
using System;
using System.Collections.Generic;
class Program
{
    static void Main()
    {
        var counts = new Dictionary<string, int>();
        foreach (string word in "C# is fun and C# is fast".ToLower().Split(' '))
            counts[word] = counts.GetValueOrDefault(word) + 1;
        Console.WriteLine(counts["c#"]);
    }
}
Result of running the code
2

Practice with the debugger

Set a breakpoint on an important assignment or condition, click Debug, and inspect the local values before stepping to the next line.

Practice with a challenge

Use what this lesson covers to solve a real problem. Each challenge runs in your browser with instant tests and an AI review.

All challenges