C# Character Variable Type

C# Character Variable Type

C#'s char type is aliasing the System.Char type. It represents a Unicode character and occupies 2 bytes.

When we talk about characters we are referring to individual letters and numbers. For example, the letter 'a' is a character, as is the visual representation of the number '1'. Such characters may be stored in a C# char variable. A char variable can contain one character and one character only.

A char literal is specified inside single quotes (‘’): char c = 'A';

For character data types we can specify literals in three ways:

  • Single quote
  • Unicode Representation
  • Escape Sequence

 

Single quote

We can specify literal to char data type as single character within single quote.

char ch = 'a';

 

Unicode Representation

We can specify char literals in Unicode representation ‘\uxxxx’. Here xxxx represents 4 hexadecimal numbers.

char ch = '\u0061';// Here /u0061 represent a.

 

Escape Sequence

Every escape character can be specified as char literals.

Escape sequences express characters that cannot be expressed literally.

An escape sequence is a backslash followed by a character with a special meaning.

For example:

char newLine = '\n';
char backSlash = '\\';

The escape sequence characters are shown in the following table.

CharMeaningValue
\'Single quote0x0027
\"Double quote0x0022
\\Backslash0x005C
\0Null0x0000
\aAlert0x0007
\bBackspace0x0008
\fForm feed0x000C
\nNew line0x000A
\rCarriage return0x000D
\tHorizontal tab0x0009
\vVertical tab0x000B

\u (or \x) escape sequence specify Unicode character using four-digit hexadecimal code:

char copyrightSymbol = '\u00A9';
char omegaSymbol     = '\u03A9';
char newLine         = '\u000A';

 

Example C# Char (Character Literals)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HelloWorld
{
    class Program
    {
        static void Main(string[] args)
        {
            // character literal within single quote
            char ch = 'a';

            // Unicode representation
            char c = '\u0061';

            Console.WriteLine(ch);
            Console.WriteLine(c);

            // Escape character literal
            Console.WriteLine("Hello World\nFrom XDevSpace\t!");
            Console.ReadLine();
        }
    }
}

Output:

Example C# Char

Note that there is a special character sequence for the Backslash (\\). Because the special characters begin with a backslash the compiler interprets any instances of a single backslash as the pre-cursor to a special character. This raises the question of what to do if you really want a backslash. The answer is to use the double backslash special constant sequence.

 

C# System.Char Type

System.Char defines a range of static methods for working with characters.

For example, You can use ToUpper to convert a char to its upper case.

You can call these through either the System.Char type or its char alias:

Example:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HelloWorld
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(System.Char.ToUpper('c'));    // C
            Console.WriteLine(char.IsWhiteSpace('\t'));     // True
            Console.ReadLine();
        }
    }
}
Output:
C# System.Char Type

ToUpper and ToLower honor the end user's locale.

System.Char and System.String provides culture-invariant versions of ToUpper and ToLower ending with the word Invariant.

These always apply English culture rules:

Console.WriteLine (char.ToUpperInvariant ('i'));    //I

This is a shortcut for:

Console.WriteLine (char.ToUpper ('i', CultureInfo.InvariantCulture))

The following table lists static methods related to categorizing characters:

Static methodCharacters included
IsLetterA-Z, a-z, and letters of other alphabets
IsUpperUppercase letters
IsLowerLowercase letters
IsDigit0-9 plus digits of other alphabets
IsLetterOrDigitLetters plus digits
IsNumberAll digits plus Unicode fractions and Roman
numeral symbols
IsSeparatorSpace plus all Unicode separator characters
IsWhiteSpaceAll separators plus \n, \r, \t, \f, and \v
IsPunctuationSymbols used for punctuation in Western and
other alphabets
IsSymbolMost other printable symbols
Static methodCharacters included
IsControlNonprintable "control" characters below 0x20, (None)
such as \r, \n, \t, \0, and characters between
0x7F and 0x9A

 

C# Char Conversions

An implicit conversion from a char to a numeric type works for the numeric types that can accommodate an unsigned short.

For other numeric types, an explicit conversion is required.