0
0
C Sharp (C#)programming~5 mins

Char type and Unicode behavior in C Sharp (C#)

Choose your learning style9 modes available
Introduction

The char type stores a single character using Unicode, letting you work with letters, numbers, and symbols from many languages.

When you want to store or manipulate a single letter or symbol.
When reading or processing text one character at a time.
When checking if a character is a digit, letter, or whitespace.
When converting characters to their numeric Unicode values.
When working with text that includes characters from different languages.
Syntax
C Sharp (C#)
char myChar = 'A';

The char type holds one character enclosed in single quotes.

Characters use Unicode, so they can represent many symbols beyond just English letters.

Examples
Stores the letter B in a char variable.
C Sharp (C#)
char letter = 'B';
Stores the character for the digit 7, not the number 7.
C Sharp (C#)
char digit = '7';
Stores a Unicode smiley face character using its code.
C Sharp (C#)
char symbol = '\u263A';
You can get the Unicode number of a character by converting it to an integer.
C Sharp (C#)
int unicodeValue = letter; // Implicit conversion to int
Sample Program

This program shows how to store characters using char and how to get their Unicode numeric values.

C Sharp (C#)
using System;

class Program
{
    static void Main()
    {
        char letter = 'A';
        char digit = '5';
        char symbol = '\u2665'; // Heart symbol

        Console.WriteLine($"Letter: {letter}");
        Console.WriteLine($"Digit: {digit}");
        Console.WriteLine($"Symbol: {symbol}");

        int letterCode = letter;
        int digitCode = digit;
        int symbolCode = symbol;

        Console.WriteLine($"Unicode of {letter}: {letterCode}");
        Console.WriteLine($"Unicode of {digit}: {digitCode}");
        Console.WriteLine($"Unicode of {symbol}: {symbolCode}");
    }
}
OutputSuccess
Important Notes

Remember to use single quotes for char values, not double quotes.

Unicode allows char to represent many characters from different languages and symbols.

Some characters like emojis need more than one char because they use surrogate pairs.

Summary

char stores one Unicode character using single quotes.

You can convert a char to an integer to get its Unicode number.

Unicode lets you work with letters and symbols from many languages easily.