Skip to main content

The following example displays the characters and their corresponding code points for characters in each unicode category.

using System;
using System.Globalization;
using System.Collections.Generic;

public class Example
{
    public static void Main()
    {
        var categories = new List<UnicodeCategory>();

        //
        // UnicodeCategory Fields:
        // https://docs.microsoft.com/en-us/dotnet/api/system.globalization.unicodecategory?view=net-5.0#fields

        categories.Add(UnicodeCategory.ClosePunctuation);
        categories.Add(UnicodeCategory.ConnectorPunctuation);
        categories.Add(UnicodeCategory.Control);
        categories.Add(UnicodeCategory.CurrencySymbol);
        categories.Add(UnicodeCategory.DashPunctuation);
        categories.Add(UnicodeCategory.DecimalDigitNumber);
        categories.Add(UnicodeCategory.EnclosingMark);
        categories.Add(UnicodeCategory.FinalQuotePunctuation);
        categories.Add(UnicodeCategory.Format);
        categories.Add(UnicodeCategory.InitialQuotePunctuation);
        categories.Add(UnicodeCategory.LetterNumber);
        categories.Add(UnicodeCategory.LineSeparator);
        categories.Add(UnicodeCategory.LowercaseLetter);
        categories.Add(UnicodeCategory.MathSymbol);
        categories.Add(UnicodeCategory.ModifierLetter);
        categories.Add(UnicodeCategory.ModifierSymbol);
        categories.Add(UnicodeCategory.NonSpacingMark);
        categories.Add(UnicodeCategory.OpenPunctuation);
        categories.Add(UnicodeCategory.OtherLetter);
        categories.Add(UnicodeCategory.OtherNotAssigned);
        categories.Add(UnicodeCategory.OtherNumber);
        categories.Add(UnicodeCategory.OtherPunctuation);
        categories.Add(UnicodeCategory.OtherSymbol);
        categories.Add(UnicodeCategory.ParagraphSeparator);
        categories.Add(UnicodeCategory.PrivateUse);
        categories.Add(UnicodeCategory.SpaceSeparator);
        categories.Add(UnicodeCategory.SpacingCombiningMark);
        categories.Add(UnicodeCategory.Surrogate);
        categories.Add(UnicodeCategory.TitlecaseLetter);
        categories.Add(UnicodeCategory.UppercaseLetter);

        foreach (var category in categories)
        {
            PrintCharAndCodePoint(category);
        }
    }

    /// <remarks>
    /// https://docs.microsoft.com/en-us/dotnet/api/system.globalization.unicodecategory?view=net-5.0#examples
    /// </remarks>
    private static void PrintCharAndCodePoint(UnicodeCategory category)
    {
        int ctr = 0;
        for (ushort codePoint = 0; codePoint < ushort.MaxValue; codePoint++)
        {
            var ch = Convert.ToChar(codePoint);
            if (CharUnicodeInfo.GetUnicodeCategory(ch) == category)
            {
                if (ctr % 5 == 0)
                {
                    Console.WriteLine();
                }
                Console.Write("{0} (U+{1:X4})     ", ch, codePoint);
                ctr++;
            }
        }

        Console.WriteLine("\n\n{0} characters are in the {1:G} category.", ctr, category);
    }
}