Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

Char Structure

Updated: December 2010

Represents a Unicode character.

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

[ComVisibleAttribute(true)]
public struct Char : IComparable, IConvertible, 
	IComparable<char>, IEquatable<char>

The Char type exposes the following members.

  NameDescription
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360CompareTo(Char)Compares this instance to a specified Char object and returns an integer that indicates whether this instance precedes, follows, or has the same position in the sort order as the specified Char value.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360CompareTo(Object)Compares this instance to a specified object and returns an integer that indicates whether this instance precedes, follows, or has the same position in the sort order as the specified object.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360Equals(Char)Returns a value indicating whether this instance is equal to the specified Char object.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360Equals(Object)Returns a value indicating whether this instance is equal to a specified object. (Overrides ValueType.Equals(Object).)
Protected methodSupported by Silverlight for Windows PhoneSupported by Xbox 360FinalizeAllows an object to try to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.)
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360GetHashCodeReturns the hash code for this instance. (Overrides ValueType.GetHashCode().)
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360GetNumericValue(Char)Converts the specified numeric Unicode character to a double-precision floating point number.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360GetNumericValue(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 Silverlight for Windows PhoneSupported by Xbox 360GetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360GetTypeCodeReturns the TypeCode for value type Char.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360GetUnicodeCategory(Char)Categorizes a specified Unicode character into a group identified by one of the UnicodeCategory values.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360GetUnicodeCategory(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 Silverlight for Windows PhoneSupported by Xbox 360IsControl(Char)Indicates whether the specified Unicode character is categorized as a control character.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360IsControl(String, Int32)Indicates whether the character at the specified position in a specified string is categorized as a control character.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360IsDigit(Char)Indicates whether the specified Unicode character is categorized as a decimal digit.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360IsDigit(String, Int32)Indicates whether the character at the specified position in a specified string is categorized as a decimal digit.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360IsLetter(Char)Indicates whether the specified Unicode character is categorized as a Unicode letter.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360IsLetter(String, Int32)Indicates whether the character at the specified position in a specified string is categorized as a Unicode letter.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360IsLetterOrDigit(Char)Indicates whether the specified Unicode character is categorized as a letter or a decimal digit.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360IsLetterOrDigit(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 Silverlight for Windows PhoneSupported by Xbox 360IsLower(Char)Indicates whether the specified Unicode character is categorized as a lowercase letter.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360IsLower(String, Int32)Indicates whether the character at the specified position in a specified string is categorized as a lowercase letter.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360IsNumber(Char)Indicates whether the specified Unicode character is categorized as a number.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360IsNumber(String, Int32)Indicates whether the character at the specified position in a specified string is categorized as a number.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360IsPunctuation(Char)Indicates whether the specified Unicode character is categorized as a punctuation mark.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360IsPunctuation(String, Int32)Indicates whether the character at the specified position in a specified string is categorized as a punctuation mark.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360IsSeparator(Char)Indicates whether the specified Unicode character is categorized as a separator character.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360IsSeparator(String, Int32)Indicates whether the character at the specified position in a specified string is categorized as a separator character.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360IsSurrogate(Char)Indicates whether the specified character has a surrogate code point.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360IsSurrogate(String, Int32)Indicates whether the character at the specified position in a specified string has a surrogate code point.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360IsSurrogatePair(Char, Char)Indicates whether the two specified Char objects form a surrogate pair.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360IsSurrogatePair(String, Int32)Indicates whether two adjacent Char objects at a specified position in a string form a surrogate pair.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360IsSymbol(Char)Indicates whether the specified Unicode character is categorized as a symbol character.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360IsSymbol(String, Int32)Indicates whether the character at the specified position in a specified string is categorized as a symbol character.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360IsUpper(Char)Indicates whether the specified Unicode character is categorized as an uppercase letter.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360IsUpper(String, Int32)Indicates whether the character at the specified position in a specified string is categorized as an uppercase letter.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360IsWhiteSpace(Char)Indicates whether the specified Unicode character is categorized as white space.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360IsWhiteSpace(String, Int32)Indicates whether the character at the specified position in a specified string is categorized as white space.
Protected methodSupported by Silverlight for Windows PhoneSupported by Xbox 360MemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360ToLower(Char)Converts the value of a Unicode character to its lowercase equivalent.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360ToLower(Char, CultureInfo)Converts the value of a specified Unicode character to its lowercase equivalent using specified culture-specific formatting information.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360ToLowerInvariantConverts the value of a Unicode character to its lowercase equivalent using the casing rules of the invariant culture.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360ToString()Converts the value of this instance to its equivalent string representation. (Overrides ValueType.ToString().)
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360ToString(Char)Converts the specified Unicode character to its equivalent string representation.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360ToString(IFormatProvider)Converts the value of this instance to its equivalent string representation using the specified culture-specific format information.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360ToUpper(Char)Converts the value of a Unicode character to its uppercase equivalent.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360ToUpper(Char, CultureInfo)Converts the value of a specified Unicode character to its uppercase equivalent using specified culture-specific formatting information.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360ToUpperInvariantConverts the value of a Unicode character to its uppercase equivalent using the casing rules of the invariant culture.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360TryParseConverts 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 Silverlight for Windows PhoneSupported by Xbox 360MaxValueRepresents the largest possible value of a Char. This field is constant.
Public fieldStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360MinValueRepresents the smallest possible value of a Char. This field is constant.
Top

  NameDescription
Explicit interface implemetationPrivate methodSupported by Silverlight for Windows PhoneSupported by Xbox 360IConvertible.ToBooleanInfrastructure. Note   This conversion is not supported. Attempting to do so throws an InvalidCastException.
Explicit interface implemetationPrivate methodSupported by Silverlight for Windows PhoneSupported by Xbox 360IConvertible.ToByteInfrastructure. For a description of this member, see ToByte.
Explicit interface implemetationPrivate methodSupported by Silverlight for Windows PhoneSupported by Xbox 360IConvertible.ToCharInfrastructure. For a description of this member, see IConvertible.ToChar.
Explicit interface implemetationPrivate methodSupported by Silverlight for Windows PhoneSupported by Xbox 360IConvertible.ToDateTimeInfrastructure. Note   This conversion is not supported. Attempting to do so throws an InvalidCastException.
Explicit interface implemetationPrivate methodSupported by Silverlight for Windows PhoneSupported by Xbox 360IConvertible.ToDecimalInfrastructure. Note   This conversion is not supported. Attempting to do so throws an InvalidCastException.
Explicit interface implemetationPrivate methodSupported by Silverlight for Windows PhoneSupported by Xbox 360IConvertible.ToDoubleInfrastructure. Note   This conversion is not supported. Attempting to do so throws an InvalidCastException.
Explicit interface implemetationPrivate methodSupported by Silverlight for Windows PhoneSupported by Xbox 360IConvertible.ToInt16Infrastructure. For a description of this member, see ToInt16.
Explicit interface implemetationPrivate methodSupported by Silverlight for Windows PhoneSupported by Xbox 360IConvertible.ToInt32Infrastructure. For a description of this member, see ToInt32.
Explicit interface implemetationPrivate methodSupported by Silverlight for Windows PhoneSupported by Xbox 360IConvertible.ToInt64Infrastructure. For a description of this member, see ToInt64.
Explicit interface implemetationPrivate methodSupported by Silverlight for Windows PhoneSupported by Xbox 360IConvertible.ToSByteInfrastructure. For a description of this member, see ToSByte.
Explicit interface implemetationPrivate methodSupported by Silverlight for Windows PhoneSupported by Xbox 360IConvertible.ToSingleInfrastructure. Note   This conversion is not supported. Attempting to do so throws an InvalidCastException.
Explicit interface implemetationPrivate methodSupported by Silverlight for Windows PhoneSupported by Xbox 360IConvertible.ToTypeInfrastructure. For a description of this member, see IConvertible.ToType.
Explicit interface implemetationPrivate methodSupported by Silverlight for Windows PhoneSupported by Xbox 360IConvertible.ToUInt16Infrastructure. For a description of this member, see ToUInt16.
Explicit interface implemetationPrivate methodSupported by Silverlight for Windows PhoneSupported by Xbox 360IConvertible.ToUInt32Infrastructure. For a description of this member, see ToUInt32.
Explicit interface implemetationPrivate methodSupported by Silverlight for Windows PhoneSupported by Xbox 360IConvertible.ToUInt64Infrastructure. For a description of this member, see 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 points 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 point is U+0061 followed by a Char object whose code point is U+0308. (The character ä can also be defined by a single Char object that has a code point of U+00E4.) The following example illustrates that the character ä consists of two Char objects.

    
    using System;
    
    public class Example
    {
       public static void Demo(System.Windows.Controls.TextBlock outputBlock)
       {
          char[] chars = { '\u0061', '\u0308' };
          string strng = new String(chars);
          outputBlock.Text += strng + Environment.NewLine;
       }
    }
    // The example produces the following output:
    //       ä
    
    
    
  • 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. For example, the UTF-16 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.

Because a single character can be represented by multiple Char objects, we recommend that you use strings instead of individual characters to represent and analyze linguistic content.

Functionality

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:

Interface Implementations

This type implements the IConvertible, IComparable, and IComparable<T> interfaces. Use the Convert class for conversions instead of this type's explicit interface member implementation of IConvertible.

The following example demonstrates some of the methods in Char.


using System;

public class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      char chA = 'A';
      char ch1 = '1';
      string str = "test string";

      outputBlock.Text += chA.CompareTo('B') + "\n";			// Output: "-1" (meaning 'A' is 1 less than 'B')
      outputBlock.Text += chA.Equals('A') + "\n";				// Output: "True"
      outputBlock.Text += Char.GetNumericValue(ch1) + "\n";	// Output: "1"
      outputBlock.Text += Char.IsControl('\t') + "\n";		// Output: "True"
      outputBlock.Text += Char.IsDigit(ch1) + "\n";			// Output: "True"
      outputBlock.Text += Char.IsLetter(',') + "\n";			// Output: "False"
      outputBlock.Text += Char.IsLower('u') + "\n";			// Output: "True"
      outputBlock.Text += Char.IsNumber(ch1) + "\n";			// Output: "True"
      outputBlock.Text += Char.IsPunctuation('.') + "\n";	// Output: "True"
      outputBlock.Text += Char.IsSeparator(str, 4) + "\n";	// Output: "True"
      outputBlock.Text += Char.IsSymbol('+') + "\n";			// Output: "True"
      outputBlock.Text += Char.IsWhiteSpace(str, 4) + "\n";	// Output: "True"
      outputBlock.Text += Char.ToLower('M') + "\n";			// Output: "m"
      outputBlock.Text += 'x'.ToString() + "\n";				// Output: "x"
   }
}


Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

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.

Date

History

Reason

December 2010

Provided additional information about the difference between a Char object and a Unicode character.

Information enhancement.

Community Additions

Show:
© 2014 Microsoft