Export (0) Print
Expand All

Char Structure

Represents a character as a UTF-16 code unit.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

[SerializableAttribute]
[ComVisibleAttribute(true)]
public value class Char : IComparable, IConvertible, 
	IComparable<wchar_t>, IEquatable<wchar_t>

The Char type exposes the following members.

  NameDescription
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsCompareTo(Char)Compares this instance to a specified Char object and indicates whether this instance precedes, follows, or appears in the same position in the sort order as the specified Char object.
Public methodSupported by the XNA FrameworkCompareTo(Object)Compares this instance to a specified object and indicates whether this instance precedes, follows, or appears in the same position in the sort order as the specified Object.
Public methodStatic memberSupported by Portable Class LibrarySupported in .NET for Windows Store appsConvertFromUtf32Converts the specified Unicode code point into a UTF-16 encoded string.
Public methodStatic memberSupported by Portable Class LibrarySupported in .NET for Windows Store appsConvertToUtf32(Char, Char)Converts the value of a UTF-16 encoded surrogate pair into a Unicode code point.
Public methodStatic memberSupported by Portable Class LibrarySupported in .NET for Windows Store appsConvertToUtf32(String, Int32)Converts the value of a UTF-16 encoded character or surrogate pair at a specified position in a string into a Unicode code point.
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsEquals(Char)Returns a value that indicates whether this instance is equal to the specified Char object.
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsEquals(Object)Returns a value that indicates whether this instance is equal to a specified object. (Overrides ValueType::Equals(Object).)
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsGetHashCodeReturns the hash code for this instance. (Overrides ValueType::GetHashCode().)
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsGetNumericValue(Char)Converts the specified numeric Unicode character to a double-precision floating point number.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsGetNumericValue(String, Int32)Converts the numeric Unicode character at the specified position in a specified string to a double-precision floating point number.
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodSupported by the XNA FrameworkGetTypeCodeReturns the TypeCode for value type Char.
Public methodStatic memberSupported by the XNA FrameworkGetUnicodeCategory(Char)Categorizes a specified Unicode character into a group identified by one of the UnicodeCategory values.
Public methodStatic memberSupported by the XNA FrameworkGetUnicodeCategory(String, Int32)Categorizes the character at the specified position in a specified string into a group identified by one of the UnicodeCategory values.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsIsControl(Char)Indicates whether the specified Unicode character is categorized as a control character.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsIsControl(String, Int32)Indicates whether the character at the specified position in a specified string is categorized as a control character.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsIsDigit(Char)Indicates whether the specified Unicode character is categorized as a decimal digit.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsIsDigit(String, Int32)Indicates whether the character at the specified position in a specified string is categorized as a decimal digit.
Public methodStatic memberSupported by Portable Class LibrarySupported in .NET for Windows Store appsIsHighSurrogate(Char)Indicates whether the specified Char object is a high surrogate.
Public methodStatic memberSupported by Portable Class LibrarySupported in .NET for Windows Store appsIsHighSurrogate(String, Int32)Indicates whether the Char object at the specified position in a string is a high surrogate.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsIsLetter(Char)Indicates whether the specified Unicode character is categorized as a Unicode letter.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsIsLetter(String, Int32)Indicates whether the character at the specified position in a specified string is categorized as a Unicode letter.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsIsLetterOrDigit(Char)Indicates whether the specified Unicode character is categorized as a letter or a decimal digit.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsIsLetterOrDigit(String, Int32)Indicates whether the character at the specified position in a specified string is categorized as a letter or a decimal digit.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsIsLower(Char)Indicates whether the specified Unicode character is categorized as a lowercase letter.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsIsLower(String, Int32)Indicates whether the character at the specified position in a specified string is categorized as a lowercase letter.
Public methodStatic memberSupported by Portable Class LibrarySupported in .NET for Windows Store appsIsLowSurrogate(Char)Indicates whether the specified Char object is a low surrogate.
Public methodStatic memberSupported by Portable Class LibrarySupported in .NET for Windows Store appsIsLowSurrogate(String, Int32)Indicates whether the Char object at the specified position in a string is a low surrogate.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsIsNumber(Char)Indicates whether the specified Unicode character is categorized as a number.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsIsNumber(String, Int32)Indicates whether the character at the specified position in a specified string is categorized as a number.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsIsPunctuation(Char)Indicates whether the specified Unicode character is categorized as a punctuation mark.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsIsPunctuation(String, Int32)Indicates whether the character at the specified position in a specified string is categorized as a punctuation mark.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsIsSeparator(Char)Indicates whether the specified Unicode character is categorized as a separator character.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsIsSeparator(String, Int32)Indicates whether the character at the specified position in a specified string is categorized as a separator character.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsIsSurrogate(Char)Indicates whether the specified character has a surrogate code unit.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsIsSurrogate(String, Int32)Indicates whether the character at the specified position in a specified string has a surrogate code unit.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsIsSurrogatePair(Char, Char)Indicates whether the two specified Char objects form a surrogate pair.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsIsSurrogatePair(String, Int32)Indicates whether two adjacent Char objects at a specified position in a string form a surrogate pair.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsIsSymbol(Char)Indicates whether the specified Unicode character is categorized as a symbol character.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsIsSymbol(String, Int32)Indicates whether the character at the specified position in a specified string is categorized as a symbol character.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsIsUpper(Char)Indicates whether the specified Unicode character is categorized as an uppercase letter.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsIsUpper(String, Int32)Indicates whether the character at the specified position in a specified string is categorized as an uppercase letter.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsIsWhiteSpace(Char)Indicates whether the specified Unicode character is categorized as white space.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsIsWhiteSpace(String, Int32)Indicates whether the character at the specified position in a specified string is categorized as white space.
Public methodStatic memberParseConverts the value of the specified string to its equivalent Unicode character.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsToLower(Char)Converts the value of a Unicode character to its lowercase equivalent.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibraryToLower(Char, CultureInfo)Converts the value of a specified Unicode character to its lowercase equivalent using specified culture-specific formatting information.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsToLowerInvariantConverts the value of a Unicode character to its lowercase equivalent using the casing rules of the invariant culture.
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsToString()Converts the value of this instance to its equivalent string representation. (Overrides ValueType::ToString().)
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsToString(Char)Converts the specified Unicode character to its equivalent string representation.
Public methodSupported by the XNA FrameworkToString(IFormatProvider)Converts the value of this instance to its equivalent string representation using the specified culture-specific format information.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsToUpper(Char)Converts the value of a Unicode character to its uppercase equivalent.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibraryToUpper(Char, CultureInfo)Converts the value of a specified Unicode character to its uppercase equivalent using specified culture-specific formatting information.
Public methodStatic memberSupported by Portable Class LibrarySupported in .NET for Windows Store appsToUpperInvariantConverts the value of a Unicode character to its uppercase equivalent using the casing rules of the invariant culture.
Public methodStatic memberSupported by Portable Class LibrarySupported in .NET for Windows Store appsTryParseConverts the value of the specified string to its equivalent Unicode character. A return code indicates whether the conversion succeeded or failed.
Top

  NameDescription
Public fieldStatic memberSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsMaxValueRepresents the largest possible value of a Char. This field is constant.
Public fieldStatic memberSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsMinValueRepresents the smallest possible value of a Char. This field is constant.
Top

  NameDescription
Explicit interface implemetationPrivate methodSupported by Portable Class LibrarySupported in .NET for Windows Store appsIComparable::CompareToCompares the current instance with another object of the same type and returns an integer that indicates whether the current instance precedes, follows, or occurs in the same position in the sort order as the other object.
Explicit interface implemetationPrivate methodSupported by the XNA FrameworkIConvertible::ToBooleanInfrastructure. Note   This conversion is not supported. Attempting to do so throws an InvalidCastException.
Explicit interface implemetationPrivate methodSupported by the XNA FrameworkIConvertible::ToByteInfrastructure. For a description of this member, see IConvertible::ToByte.
Explicit interface implemetationPrivate methodSupported by the XNA FrameworkIConvertible::ToCharInfrastructure. For a description of this member, see IConvertible::ToChar.
Explicit interface implemetationPrivate methodSupported by the XNA FrameworkIConvertible::ToDateTimeInfrastructure. Note   This conversion is not supported. Attempting to do so throws an InvalidCastException.
Explicit interface implemetationPrivate methodSupported by the XNA FrameworkIConvertible::ToDecimalInfrastructure. Note   This conversion is not supported. Attempting to do so throws an InvalidCastException.
Explicit interface implemetationPrivate methodSupported by the XNA FrameworkIConvertible::ToDoubleInfrastructure. Note   This conversion is not supported. Attempting to do so throws an InvalidCastException.
Explicit interface implemetationPrivate methodSupported by the XNA FrameworkIConvertible::ToInt16Infrastructure. For a description of this member, see IConvertible::ToInt16.
Explicit interface implemetationPrivate methodSupported by the XNA FrameworkIConvertible::ToInt32Infrastructure. For a description of this member, see IConvertible::ToInt32.
Explicit interface implemetationPrivate methodSupported by the XNA FrameworkIConvertible::ToInt64Infrastructure. For a description of this member, see IConvertible::ToInt64.
Explicit interface implemetationPrivate methodSupported by the XNA FrameworkIConvertible::ToSByteInfrastructure. For a description of this member, see IConvertible::ToSByte.
Explicit interface implemetationPrivate methodSupported by the XNA FrameworkIConvertible::ToSingleInfrastructure. Note   This conversion is not supported. Attempting to do so throws an InvalidCastException.
Explicit interface implemetationPrivate methodSupported by the XNA FrameworkIConvertible::ToTypeInfrastructure. For a description of this member, see IConvertible::ToType.
Explicit interface implemetationPrivate methodSupported by the XNA FrameworkIConvertible::ToUInt16Infrastructure. For a description of this member, see IConvertible::ToUInt16.
Explicit interface implemetationPrivate methodSupported by the XNA FrameworkIConvertible::ToUInt32Infrastructure. For a description of this member, see IConvertible::ToUInt32.
Explicit interface implemetationPrivate methodSupported by the XNA FrameworkIConvertible::ToUInt64Infrastructure. For a description of this member, see IConvertible::ToUInt64.
Top

The .NET Framework 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:

  • Glyphs, which may consist of a single character or of a base character followed by one or more combining characters. For example, the character ä is represented by a Char object whose code unit is U+0061 followed by a Char object whose code unit is U+0308. (The character ä can also be defined by a single Char object that has a code unit of U+00E4.) The following example illustrates that the character ä consists of two Char objects.

    No code example is currently available or this language may not be supported.
  • Characters outside the Unicode Basic Multilingual Plane (BMP). Unicode supports sixteen planes in addition to the BMP, which represents plane 0. A Unicode code point is represented in UTF-32 by a 21-bit value that includes the plane. For example, U+1D160 represents the MUSICAL SYMBOL EIGHTH NOTE character. Because UTF-16 encoding has only 16 bits, characters outside the BMP are represented by surrogate pairs in UTF-16. The following example illustrates that the UTF-32 equivalent of U+1D160, the MUSICAL SYMBOL EIGHTH NOTE character, is U+D834 U+DD60. U+D834 is the high surrogate; high surrogates range from U+D800 through U+DBFF. U+DD60 is the low surrogate; low surrogates range from U+DC00 through U+DFFF.

    No code example is currently available or this language may not be supported.

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.

No code example is currently available or this language may not be supported.

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

  • You can work with a String object in its entirety instead of working with its individual characters to represent and analyze linguistic content.

  • You can use the StringInfo class to work with text elements instead of individual Char objects. The following example uses the StringInfo object to count the number of text elements in a string that consists of the Aegean numbers zero through nine. Because it considers a surrogate pair a single character, it correctly reports that the string contains ten characters.

    No code example is currently available or this language may not be supported.
  • If a string contains a base character that has one or more combining characters, you can call the String::Normalize method to convert the substring to a single UTF-16 encoded code unit. The following example calls the String::Normalize method to convert the base character U+0061 (LATIN SMALL LETTER A) and combining character U+0308 (COMBINING DIAERESIS) to U+00E4 (LATIN SMALL LETTER A WITH DIAERESIS).

    No code example is currently available or this language may not be supported.

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:

To do this

Use these System.Char methods

Compare Char objects

CompareTo and Equals

Convert a code point to a string

ConvertFromUtf32

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

ConvertToUtf32

Get the Unicode category of a character

GetUnicodeCategory

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

IsControl, IsDigit, IsHighSurrogate, IsLetter, IsLetterOrDigit, IsLower, IsLowSurrogate, IsNumber, IsPunctuation, IsSeparator, IsSurrogate, IsSurrogatePair, IsSymbol, IsUpper, and IsWhiteSpace

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

GetNumericValue

Convert a character in a string into a Char object

Parse and TryParse

Convert a Char object to a String object

ToString

Change the case of a Char object

ToLower, ToLowerInvariant, ToUpper, and ToUpperInvariant

The following code example demonstrates some of the methods in Char.

using namespace System;
int main()
{
   char chA = 'A';
   char ch1 = '1';
   String^ str =  "test string";
   Console::WriteLine( chA.CompareTo( 'B' ) ); // Output: "-1" (meaning 'A' is 1 less than 'B')
   Console::WriteLine( chA.Equals( 'A' ) ); // Output: "True"
   Console::WriteLine( Char::GetNumericValue( ch1 ) ); // Output: "1"
   Console::WriteLine( Char::IsControl( '\t' ) ); // Output: "True"
   Console::WriteLine( Char::IsDigit( ch1 ) ); // Output: "True"
   Console::WriteLine( Char::IsLetter( ',' ) ); // Output: "False"
   Console::WriteLine( Char::IsLower( 'u' ) ); // Output: "True"
   Console::WriteLine( Char::IsNumber( ch1 ) ); // Output: "True"
   Console::WriteLine( Char::IsPunctuation( '.' ) ); // Output: "True"
   Console::WriteLine( Char::IsSeparator( str, 4 ) ); // Output: "True"
   Console::WriteLine( Char::IsSymbol( '+' ) ); // Output: "True"
   Console::WriteLine( Char::IsWhiteSpace( str, 4 ) ); // Output: "True"
   Console::WriteLine( Char::Parse(  "S" ) ); // Output: "S"
   Console::WriteLine( Char::ToLower( 'M' ) ); // Output: "m"
   Console::WriteLine( 'x' ); // Output: "x"
}

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone 8, Silverlight 8.1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

All members of this type are thread safe. Members that appear to modify instance state actually return a new instance initialized with the new value. As with any other type, reading and writing to a shared variable that contains an instance of this type must be protected by a lock to guarantee thread safety.

Show:
© 2014 Microsoft