0 out of 5 rated this helpful - Rate this topic

Char Structure

Updated: December 2010

Represents a character as a UTF-16 code unit.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)
[SerializableAttribute]
[ComVisibleAttribute(true)]
public struct Char : IComparable, IConvertible, 
	IComparable<char>, IEquatable<char>

The Char type exposes the following members.

  Name Description
Public method Supported by the XNA Framework Supported by Portable Class Library CompareTo(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 method Supported by the XNA Framework CompareTo(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 method Static member ConvertFromUtf32 Converts the specified Unicode code point into a UTF-16 encoded string.
Public method Static member ConvertToUtf32(Char, Char) Converts the value of a UTF-16 encoded surrogate pair into a Unicode code point.
Public method Static member ConvertToUtf32(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 method Supported by the XNA Framework Supported by Portable Class Library Equals(Char) Returns a value that indicates whether this instance is equal to the specified Char object.
Public method Supported by the XNA Framework Supported by Portable Class Library Equals(Object) Returns a value that indicates whether this instance is equal to a specified object. (Overrides ValueType.Equals(Object).)
Protected method Supported by the XNA Framework Supported by Portable Class Library Finalize Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public method Supported by the XNA Framework Supported by Portable Class Library GetHashCode Returns the hash code for this instance. (Overrides ValueType.GetHashCode().)
Public method Static member Supported by the XNA Framework Supported by Portable Class Library GetNumericValue(Char) Converts the specified numeric Unicode character to a double-precision floating point number.
Public method Static member Supported by the XNA Framework Supported by Portable Class Library GetNumericValue(String, Int32) Converts the numeric Unicode character at the specified position in a specified string to a double-precision floating point number.
Public method Supported by the XNA Framework Supported by Portable Class Library GetType Gets the Type of the current instance. (Inherited from Object.)
Public method Supported by the XNA Framework GetTypeCode Returns the TypeCode for value type Char.
Public method Static member Supported by the XNA Framework Supported by Portable Class Library GetUnicodeCategory(Char) Categorizes a specified Unicode character into a group identified by one of the UnicodeCategory values.
Public method Static member Supported by the XNA Framework Supported by Portable Class Library GetUnicodeCategory(String, Int32) Categorizes the character at the specified position in a specified string into a group identified by one of the UnicodeCategory values.
Public method Static member Supported by the XNA Framework Supported by Portable Class Library IsControl(Char) Indicates whether the specified Unicode character is categorized as a control character.
Public method Static member Supported by the XNA Framework Supported by Portable Class Library IsControl(String, Int32) Indicates whether the character at the specified position in a specified string is categorized as a control character.
Public method Static member Supported by the XNA Framework Supported by Portable Class Library IsDigit(Char) Indicates whether the specified Unicode character is categorized as a decimal digit.
Public method Static member Supported by the XNA Framework Supported by Portable Class Library IsDigit(String, Int32) Indicates whether the character at the specified position in a specified string is categorized as a decimal digit.
Public method Static member IsHighSurrogate(Char) Indicates whether the specified Char object is a high surrogate.
Public method Static member IsHighSurrogate(String, Int32) Indicates whether the Char object at the specified position in a string is a high surrogate.
Public method Static member Supported by the XNA Framework Supported by Portable Class Library IsLetter(Char) Indicates whether the specified Unicode character is categorized as a Unicode letter.
Public method Static member Supported by the XNA Framework Supported by Portable Class Library IsLetter(String, Int32) Indicates whether the character at the specified position in a specified string is categorized as a Unicode letter.
Public method Static member Supported by the XNA Framework Supported by Portable Class Library IsLetterOrDigit(Char) Indicates whether the specified Unicode character is categorized as a letter or a decimal digit.
Public method Static member Supported by the XNA Framework Supported by Portable Class Library IsLetterOrDigit(String, Int32) Indicates whether the character at the specified position in a specified string is categorized as a letter or a decimal digit.
Public method Static member Supported by the XNA Framework Supported by Portable Class Library IsLower(Char) Indicates whether the specified Unicode character is categorized as a lowercase letter.
Public method Static member Supported by the XNA Framework Supported by Portable Class Library IsLower(String, Int32) Indicates whether the character at the specified position in a specified string is categorized as a lowercase letter.
Public method Static member IsLowSurrogate(Char) Indicates whether the specified Char object is a low surrogate.
Public method Static member IsLowSurrogate(String, Int32) Indicates whether the Char object at the specified position in a string is a low surrogate.
Public method Static member Supported by the XNA Framework Supported by Portable Class Library IsNumber(Char) Indicates whether the specified Unicode character is categorized as a number.
Public method Static member Supported by the XNA Framework Supported by Portable Class Library IsNumber(String, Int32) Indicates whether the character at the specified position in a specified string is categorized as a number.
Public method Static member Supported by the XNA Framework Supported by Portable Class Library IsPunctuation(Char) Indicates whether the specified Unicode character is categorized as a punctuation mark.
Public method Static member Supported by the XNA Framework Supported by Portable Class Library IsPunctuation(String, Int32) Indicates whether the character at the specified position in a specified string is categorized as a punctuation mark.
Public method Static member Supported by the XNA Framework Supported by Portable Class Library IsSeparator(Char) Indicates whether the specified Unicode character is categorized as a separator character.
Public method Static member Supported by the XNA Framework Supported by Portable Class Library IsSeparator(String, Int32) Indicates whether the character at the specified position in a specified string is categorized as a separator character.
Public method Static member Supported by the XNA Framework Supported by Portable Class Library IsSurrogate(Char) Indicates whether the specified character has a surrogate code unit.
Public method Static member Supported by the XNA Framework Supported by Portable Class Library IsSurrogate(String, Int32) Indicates whether the character at the specified position in a specified string has a surrogate code unit.
Public method Static member Supported by the XNA Framework Supported by Portable Class Library IsSurrogatePair(Char, Char) Indicates whether the two specified Char objects form a surrogate pair.
Public method Static member Supported by the XNA Framework Supported by Portable Class Library IsSurrogatePair(String, Int32) Indicates whether two adjacent Char objects at a specified position in a string form a surrogate pair.
Public method Static member Supported by the XNA Framework Supported by Portable Class Library IsSymbol(Char) Indicates whether the specified Unicode character is categorized as a symbol character.
Public method Static member Supported by the XNA Framework Supported by Portable Class Library IsSymbol(String, Int32) Indicates whether the character at the specified position in a specified string is categorized as a symbol character.
Public method Static member Supported by the XNA Framework Supported by Portable Class Library IsUpper(Char) Indicates whether the specified Unicode character is categorized as an uppercase letter.
Public method Static member Supported by the XNA Framework Supported by Portable Class Library IsUpper(String, Int32) Indicates whether the character at the specified position in a specified string is categorized as an uppercase letter.
Public method Static member Supported by the XNA Framework Supported by Portable Class Library IsWhiteSpace(Char) Indicates whether the specified Unicode character is categorized as white space.
Public method Static member Supported by the XNA Framework Supported by Portable Class Library IsWhiteSpace(String, Int32) Indicates whether the character at the specified position in a specified string is categorized as white space.
Protected method Supported by the XNA Framework Supported by Portable Class Library MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Public method Static member Parse Converts the value of the specified string to its equivalent Unicode character.
Public method Static member Supported by the XNA Framework ToLower(Char) Converts the value of a Unicode character to its lowercase equivalent.
Public method Static member Supported by the XNA Framework Supported by Portable Class Library ToLower(Char, CultureInfo) Converts the value of a specified Unicode character to its lowercase equivalent using specified culture-specific formatting information.
Public method Static member Supported by the XNA Framework Supported by Portable Class Library ToLowerInvariant Converts the value of a Unicode character to its lowercase equivalent using the casing rules of the invariant culture.
Public method Supported by the XNA Framework Supported by Portable Class Library ToString() Converts the value of this instance to its equivalent string representation. (Overrides ValueType.ToString().)
Public method Static member Supported by the XNA Framework Supported by Portable Class Library ToString(Char) Converts the specified Unicode character to its equivalent string representation.
Public method Supported by the XNA Framework Supported by Portable Class Library ToString(IFormatProvider) Converts the value of this instance to its equivalent string representation using the specified culture-specific format information.
Public method Static member Supported by the XNA Framework ToUpper(Char) Converts the value of a Unicode character to its uppercase equivalent.
Public method Static member Supported by the XNA Framework Supported by Portable Class Library ToUpper(Char, CultureInfo) Converts the value of a specified Unicode character to its uppercase equivalent using specified culture-specific formatting information.
Public method Static member Supported by Portable Class Library ToUpperInvariant Converts the value of a Unicode character to its uppercase equivalent using the casing rules of the invariant culture.
Public method Static member Supported by Portable Class Library TryParse Converts the value of the specified string to its equivalent Unicode character. A return code indicates whether the conversion succeeded or failed.
Top
  Name Description
Public field Static member Supported by the XNA Framework Supported by Portable Class Library MaxValue Represents the largest possible value of a Char. This field is constant.
Public field Static member Supported by the XNA Framework Supported by Portable Class Library MinValue Represents the smallest possible value of a Char. This field is constant.
Top
  Name Description
Explicit interface implemetation Private method Supported by the XNA Framework IConvertible.ToBoolean Infrastructure. Note   This conversion is not supported. Attempting to do so throws an InvalidCastException.
Explicit interface implemetation Private method Supported by the XNA Framework IConvertible.ToByte Infrastructure. For a description of this member, see IConvertible.ToByte.
Explicit interface implemetation Private method Supported by the XNA Framework IConvertible.ToChar Infrastructure. For a description of this member, see IConvertible.ToChar.
Explicit interface implemetation Private method Supported by the XNA Framework IConvertible.ToDateTime Infrastructure. Note   This conversion is not supported. Attempting to do so throws an InvalidCastException.
Explicit interface implemetation Private method Supported by the XNA Framework IConvertible.ToDecimal Infrastructure. Note   This conversion is not supported. Attempting to do so throws an InvalidCastException.
Explicit interface implemetation Private method Supported by the XNA Framework IConvertible.ToDouble Infrastructure. Note   This conversion is not supported. Attempting to do so throws an InvalidCastException.
Explicit interface implemetation Private method Supported by the XNA Framework IConvertible.ToInt16 Infrastructure. For a description of this member, see IConvertible.ToInt16.
Explicit interface implemetation Private method Supported by the XNA Framework IConvertible.ToInt32 Infrastructure. For a description of this member, see IConvertible.ToInt32.
Explicit interface implemetation Private method Supported by the XNA Framework IConvertible.ToInt64 Infrastructure. For a description of this member, see IConvertible.ToInt64.
Explicit interface implemetation Private method Supported by the XNA Framework IConvertible.ToSByte Infrastructure. For a description of this member, see IConvertible.ToSByte.
Explicit interface implemetation Private method Supported by the XNA Framework IConvertible.ToSingle Infrastructure. Note   This conversion is not supported. Attempting to do so throws an InvalidCastException.
Explicit interface implemetation Private method Supported by the XNA Framework IConvertible.ToType Infrastructure. For a description of this member, see IConvertible.ToType.
Explicit interface implemetation Private method Supported by the XNA Framework IConvertible.ToUInt16 Infrastructure. For a description of this member, see IConvertible.ToUInt16.
Explicit interface implemetation Private method Supported by the XNA Framework IConvertible.ToUInt32 Infrastructure. For a description of this member, see IConvertible.ToUInt32.
Explicit interface implemetation Private method Supported by the XNA Framework IConvertible.ToUInt64 Infrastructure. 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.

    
    using System;
    using System.IO;
    
    public class Example
    {
       public static void Main()
       {
          StreamWriter sw = new StreamWriter("chars1.txt");
          char[] chars = { '\u0061', '\u0308' };
          string strng = new String(chars);
          sw.WriteLine(strng); 
          sw.Close();
       }
    }
    // 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. 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.

    
    using System;
    using System.IO;
    
    public class Example
    {
       public static void Main()
       {
          StreamWriter sw = new StreamWriter(@".\chars2.txt");
          int utf32 = 0x1D160;
          string surrogate = Char.ConvertFromUtf32(utf32);
          sw.WriteLine("U+{0:X6} UTF-32 = {1} ({2}) UTF-16", 
                       utf32, surrogate, ShowCodePoints(surrogate));
          sw.Close();                    
       }
    
       private static string ShowCodePoints(string value)
       {
          string retval = null;
          foreach (var ch in value)
             retval += String.Format("U+{0:X4} ", Convert.ToUInt16(ch));
    
          return retval.Trim();
       }
    }
    // The example produces the following output:
    //       U+01D160 UTF-32 = ð (U+D834 U+DD60) UTF-16
    
    
    

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:

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


using System;

public class CharStructureSample {
	public static void 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'.ToString());				// Output: "x"
	}
}


.NET Framework

Supported in: 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

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

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.

Date

History

Reason

December 2010

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

Information enhancement.

Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Code point vs. character
Strictly speaking, it would be more accurate to describe the char datype as representing a UTF-16 code point than a Unicode character.  In most cases these are equivalant, but under some circumstances the distinction is significant.   Possible values of a Char include include two noncharacter values at the end of the range which are designated by Unicode as special signalling devices (sentinel values) or as working values durring character string formatting:
  • U+FFFE - Reserved non-character:  may be used as a sentinel value marking the beginning of a stream
  • U+FFFF - Reserved non-character:  may be used as a sentinel value marking the end of a stream
  • U+FDD0 to U+FDEF - Reserved non-characters

Also Char values in the following ranges are reserved for use in surregate pairs to represent Unicode characters outside the 16-bit range.   Such extended characters are useful in fully supporting characters for languages including Chinese (GB 18030)

  • U+D800 to U+DBFF - High Surrogate Area
  • U+DC00 to U+DFFF - Low Surrogate area

Certain combinations of currogate codes represent additional non-character or invalid combinations.

Referencs: