Delen via


char (C# Reference)

The char keyword is used to declare a Unicode character in the range indicated in the following table. Unicode characters are 16-bit characters that are used to represent most of the known written languages throughout the world.

Type

Range

Size

.NET Framework type

char

U+0000 to U+ffff

Unicode 16-bit character

System.Char

Literals

Constants of the char type can be written as character literals, hexadecimal escape sequence, or Unicode representation. You can also cast the integral character codes. In the following example four char variables are initialized with the same character X:

char[] chars = new char[4];

chars[0] = 'X';        // Character literal
chars[1] = '\x0058';   // Hexadecimal
chars[2] = (char)88;   // Cast from integral type
chars[3] = '\u0058';   // Unicode 

foreach (char c in chars)
{
    Console.Write(c + " ");
}
// Output: X X X X

Conversions

A char can be implicitly converted to ushort, int, uint, long, ulong, float, double, or decimal. However, there are no implicit conversions from other types to the char type.

The System.Char type provides several static methods for working with char values.

C# Language Specification

For more information, see the following sections in the C# Language Specification:

  • 1.3 Types and Variables

  • 2.4.4.4 Character Literals

  • 4.1.5 Integral Types

See Also

Concepts

C# Programming Guide

Reference

C# Keywords

Integral Types Table (C# Reference)

Built-In Types Table (C# Reference)

Implicit Numeric Conversions Table (C# Reference)

Explicit Numeric Conversions Table (C# Reference)

Char

String Basics (C# Programming Guide)

Other Resources

C# Reference