CodShot
C# Public

Count Words with Dictionary

Count repeated words with a Dictionary in a simple console example.

#csharp #dictionary #string #count
C#
using System;
using System.Collections.Generic;

string text = "csharp is useful and csharp is fun";
string[] words = text.ToLower().Split(' ');

Dictionary<string, int> counts = new();

foreach (string word in words)
{
    if (!counts.ContainsKey(word))
    {
        counts[word] = 0;
    }

    counts[word]++;
}

foreach (var item in counts)
{
    Console.WriteLine($"{item.Key}: {item.Value}");
}

Notes

Dictionary is a good fit when you need key-value counts.

Snippet actions

CodShot user
CodShot user
Updated: 2026-06-08 14:33
Public snippets
0
Forks
CodShot AI Help
Ask about CodShot features, plans, workspace, AI limits, referrals, and public help topics.
Hi! I can help you understand how CodShot works. What would you like to know?
For account-specific or sensitive issues, please open a support ticket. Open support