Export (0) Print
Expand All
8 out of 10 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 .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:

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 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"
	}
}

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.

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

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

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 3.0, 2.0, 1.0

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)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.