System.Char Structure

Represents a character as a UTF-16 code unit.

See Also: Char Members

Syntax

[System.Runtime.InteropServices.ComVisible(true)]
public struct Char : IComparable, IComparable<char>, IConvertible, IEquatable<char>

Remarks

The dnprdnshort uses the char structure to represent a Unicode character. The Unicode Standard identifies each Unicode character with a unique 21-bit scalar number called a code point, and defines the UTF-16 encoding form that specifies how a code point is encoded into a sequence of one or more 16-bit values. Each 16-bit value ranges from hexadecimal 0x0000 through 0xFFFF and is stored in a char structure. The value of a char object is its 16-bit numeric (ordinal) value.

Char Objects, Unicode Characters, and Strings

A string object is a sequential collection of char structures that represents a string of text. Most Unicode characters can be represented by a single char object, but a character that is encoded as a base character, surrogate pair, and/or combining character sequence is represented by multiple char objects. For this reason, a char structure in a string object is not necessarily equivalent to a single Unicode character.

Multiple 16-bit code units are used to represent single Unicode characters in the following cases:

Characters and Text Elements

Because a single character can be represented by multiple char objects, it is not always meaningful to work with individual char objects. For instance, the following example converts the Unicode code points that represent the Aegean numbers zero through 9 to UTF-16 encoded code units. Because it erroneously equates char objects with characters, it inaccurately reports that the resulting string has 20 characters.

code reference: System.Char.Class#3

You can do the following to avoid the assumption that a char object represents a single character.

Common Operations

The char structure provides methods to compare char objects, convert the value of the current char object to an object of another type, and determine the Unicode category of a char object:

Compare char objects

erload:System.Char.CompareTo and erload:System.Char.Equals

Convert a code point to a string

char.ConvertFromUtf32(int)

Convert a char object or a surrogate pair of char objects to a code point

erload:System.Char.ConvertToUtf32

Get the Unicode category of a character

erload:System.Char.GetUnicodeCategory

Determine whether a character is in a particular Unicode category such as digit, letter, punctuation, control character, and so on

erload:System.Char.IsControl, erload:System.Char.IsDigit, erload:System.Char.IsHighSurrogate, erload:System.Char.IsLetter, erload:System.Char.IsLetterOrDigit, erload:System.Char.IsLower, erload:System.Char.IsLowSurrogate, erload:System.Char.IsNumber, erload:System.Char.IsPunctuation, erload:System.Char.IsSeparator, erload:System.Char.IsSurrogate, erload:System.Char.IsSurrogatePair, erload:System.Char.IsSymbol, erload:System.Char.IsUpper, and erload:System.Char.IsWhiteSpace

Convert a char object that represents a number to a numeric value type

erload:System.Char.GetNumericValue

Convert a character in a string into a char object

char.Parse(string) and char.TryParse(string, Char@)

Convert a char object to a string object

erload:System.Char.ToString

Change the case of a char object

erload:System.Char.ToLower, char.ToLowerInvariant(char), erload:System.Char.ToUpper, and char.ToUpperInvariant(char)

Thread Safety

This type is safe for multithreaded operations.

Requirements

Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0, 4.0.0.0