Char Structure
 

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>
[SerializableAttribute]
[ComVisibleAttribute(true)]
public value struct Char : IComparable, IConvertible, IComparable<wchar_t>, 
	IEquatable<wchar_t>
[<Sealed>]
[<SerializableAttribute>]
[<ComVisibleAttribute(true)>]
type Char = 
    struct
        interface IComparable
        interface IConvertible
        interface IComparable<char>
        interface IEquatable<char>
    end
<SerializableAttribute>
<ComVisibleAttribute(True)>
Public Structure Char
	Implements IComparable, IConvertible, IComparable(OfChar), IEquatable(OfChar)
NameDescription
System_CAPS_pubmethod CompareTo

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.

System_CAPS_pubmethod CompareTo

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.

System_CAPS_pubmethod System_CAPS_static ConvertFromUtf32

Converts the specified Unicode code point into a UTF-16 encoded string.

System_CAPS_pubmethod System_CAPS_static ConvertToUtf32

Converts the value of a UTF-16 encoded surrogate pair into a Unicode code point.

System_CAPS_pubmethod System_CAPS_static ConvertToUtf32

Converts the value of a UTF-16 encoded character or surrogate pair at a specified position in a string into a Unicode code point.

System_CAPS_pubmethod Equals

Returns a value that indicates whether this instance is equal to the specified Char object.

System_CAPS_pubmethod Equals

Returns a value that indicates whether this instance is equal to a specified object.(Overrides ValueTypeEquals.)

System_CAPS_pubmethod GetHashCode

Returns the hash code for this instance.(Overrides ValueTypeGetHashCode.)

System_CAPS_pubmethod System_CAPS_static GetNumericValue

Converts the specified numeric Unicode character to a double-precision floating point number.

System_CAPS_pubmethod System_CAPS_static GetNumericValue

Converts the numeric Unicode character at the specified position in a specified string to a double-precision floating point number.

System_CAPS_pubmethod GetType

Gets the Type of the current instance.(Inherited from Object.)

System_CAPS_pubmethod GetTypeCode

Returns the TypeCode for value type Char.

System_CAPS_pubmethod System_CAPS_static GetUnicodeCategory

Categorizes a specified Unicode character into a group identified by one of the UnicodeCategory values.

System_CAPS_pubmethod System_CAPS_static GetUnicodeCategory

Categorizes the character at the specified position in a specified string into a group identified by one of the UnicodeCategory values.

System_CAPS_pubmethod System_CAPS_static IsControl

Indicates whether the specified Unicode character is categorized as a control character.

System_CAPS_pubmethod System_CAPS_static IsControl

Indicates whether the character at the specified position in a specified string is categorized as a control character.

System_CAPS_pubmethod System_CAPS_static IsDigit

Indicates whether the specified Unicode character is categorized as a decimal digit.

System_CAPS_pubmethod System_CAPS_static IsDigit

Indicates whether the character at the specified position in a specified string is categorized as a decimal digit.

System_CAPS_pubmethod System_CAPS_static IsHighSurrogate

Indicates whether the specified Char object is a high surrogate.

System_CAPS_pubmethod System_CAPS_static IsHighSurrogate

Indicates whether the Char object at the specified position in a string is a high surrogate.

System_CAPS_pubmethod System_CAPS_static IsLetter

Indicates whether the specified Unicode character is categorized as a Unicode letter.

System_CAPS_pubmethod System_CAPS_static IsLetter

Indicates whether the character at the specified position in a specified string is categorized as a Unicode letter.

System_CAPS_pubmethod System_CAPS_static IsLetterOrDigit

Indicates whether the specified Unicode character is categorized as a letter or a decimal digit.

System_CAPS_pubmethod System_CAPS_static IsLetterOrDigit

Indicates whether the character at the specified position in a specified string is categorized as a letter or a decimal digit.

System_CAPS_pubmethod System_CAPS_static IsLower

Indicates whether the specified Unicode character is categorized as a lowercase letter.

System_CAPS_pubmethod System_CAPS_static IsLower

Indicates whether the character at the specified position in a specified string is categorized as a lowercase letter.

System_CAPS_pubmethod System_CAPS_static IsLowSurrogate

Indicates whether the specified Char object is a low surrogate.

System_CAPS_pubmethod System_CAPS_static IsLowSurrogate

Indicates whether the Char object at the specified position in a string is a low surrogate.

System_CAPS_pubmethod System_CAPS_static IsNumber

Indicates whether the specified Unicode character is categorized as a number.

System_CAPS_pubmethod System_CAPS_static IsNumber

Indicates whether the character at the specified position in a specified string is categorized as a number.

System_CAPS_pubmethod System_CAPS_static IsPunctuation

Indicates whether the specified Unicode character is categorized as a punctuation mark.

System_CAPS_pubmethod System_CAPS_static IsPunctuation

Indicates whether the character at the specified position in a specified string is categorized as a punctuation mark.

System_CAPS_pubmethod System_CAPS_static IsSeparator

Indicates whether the specified Unicode character is categorized as a separator character.

System_CAPS_pubmethod System_CAPS_static IsSeparator

Indicates whether the character at the specified position in a specified string is categorized as a separator character.

System_CAPS_pubmethod System_CAPS_static IsSurrogate

Indicates whether the specified character has a surrogate code unit.

System_CAPS_pubmethod System_CAPS_static IsSurrogate

Indicates whether the character at the specified position in a specified string has a surrogate code unit.

System_CAPS_pubmethod System_CAPS_static IsSurrogatePair

Indicates whether the two specified Char objects form a surrogate pair.

System_CAPS_pubmethod System_CAPS_static IsSurrogatePair

Indicates whether two adjacent Char objects at a specified position in a string form a surrogate pair.

System_CAPS_pubmethod System_CAPS_static IsSymbol

Indicates whether the specified Unicode character is categorized as a symbol character.

System_CAPS_pubmethod System_CAPS_static IsSymbol

Indicates whether the character at the specified position in a specified string is categorized as a symbol character.

System_CAPS_pubmethod System_CAPS_static IsUpper

Indicates whether the specified Unicode character is categorized as an uppercase letter.

System_CAPS_pubmethod System_CAPS_static IsUpper

Indicates whether the character at the specified position in a specified string is categorized as an uppercase letter.

System_CAPS_pubmethod System_CAPS_static IsWhiteSpace

Indicates whether the specified Unicode character is categorized as white space.

System_CAPS_pubmethod System_CAPS_static IsWhiteSpace

Indicates whether the character at the specified position in a specified string is categorized as white space.

System_CAPS_pubmethod System_CAPS_static Parse

Converts the value of the specified string to its equivalent Unicode character.

System_CAPS_pubmethod System_CAPS_static ToLower

Converts the value of a Unicode character to its lowercase equivalent.

System_CAPS_pubmethod System_CAPS_static ToLower

Converts the value of a specified Unicode character to its lowercase equivalent using specified culture-specific formatting information.

System_CAPS_pubmethod System_CAPS_static ToLowerInvariant

Converts the value of a Unicode character to its lowercase equivalent using the casing rules of the invariant culture.

System_CAPS_pubmethod ToString

Converts the value of this instance to its equivalent string representation.(Overrides ValueTypeToString.)

System_CAPS_pubmethod System_CAPS_static ToString

Converts the specified Unicode character to its equivalent string representation.

System_CAPS_pubmethod ToString

Converts the value of this instance to its equivalent string representation using the specified culture-specific format information.

System_CAPS_pubmethod System_CAPS_static ToUpper

Converts the value of a Unicode character to its uppercase equivalent.

System_CAPS_pubmethod System_CAPS_static ToUpper

Converts the value of a specified Unicode character to its uppercase equivalent using specified culture-specific formatting information.

System_CAPS_pubmethod System_CAPS_static ToUpperInvariant

Converts the value of a Unicode character to its uppercase equivalent using the casing rules of the invariant culture.

System_CAPS_pubmethod System_CAPS_static TryParse

Converts the value of the specified string to its equivalent Unicode character. A return code indicates whether the conversion succeeded or failed.

NameDescription
System_CAPS_pubfield System_CAPS_static MaxValue

Represents the largest possible value of a Char. This field is constant.

System_CAPS_pubfield System_CAPS_static MinValue

Represents the smallest possible value of a Char. This field is constant.

NameDescription
System_CAPS_pubinterface System_CAPS_privmethod IConvertibleToBoolean

This API supports the product infrastructure and is not intended to be used directly from your code. Note   This conversion is not supported. Attempting to do so throws an InvalidCastException.

System_CAPS_pubinterface System_CAPS_privmethod IConvertibleToByte

This API supports the product infrastructure and is not intended to be used directly from your code. For a description of this member, see IConvertibleToByte.

System_CAPS_pubinterface System_CAPS_privmethod IConvertibleToChar

This API supports the product infrastructure and is not intended to be used directly from your code. For a description of this member, see IConvertibleToChar.

System_CAPS_pubinterface System_CAPS_privmethod IConvertibleToDateTime

This API supports the product infrastructure and is not intended to be used directly from your code. Note   This conversion is not supported. Attempting to do so throws an InvalidCastException.

System_CAPS_pubinterface System_CAPS_privmethod IConvertibleToDecimal

This API supports the product infrastructure and is not intended to be used directly from your code. Note   This conversion is not supported. Attempting to do so throws an InvalidCastException.

System_CAPS_pubinterface System_CAPS_privmethod IConvertibleToDouble

This API supports the product infrastructure and is not intended to be used directly from your code. Note   This conversion is not supported. Attempting to do so throws an InvalidCastException.

System_CAPS_pubinterface System_CAPS_privmethod IConvertibleToInt16

This API supports the product infrastructure and is not intended to be used directly from your code. For a description of this member, see IConvertibleToInt16.

System_CAPS_pubinterface System_CAPS_privmethod IConvertibleToInt32

This API supports the product infrastructure and is not intended to be used directly from your code. For a description of this member, see IConvertibleToInt32.

System_CAPS_pubinterface System_CAPS_privmethod IConvertibleToInt64

This API supports the product infrastructure and is not intended to be used directly from your code. For a description of this member, see IConvertibleToInt64.

System_CAPS_pubinterface System_CAPS_privmethod IConvertibleToSByte

This API supports the product infrastructure and is not intended to be used directly from your code. For a description of this member, see IConvertibleToSByte.

System_CAPS_pubinterface System_CAPS_privmethod IConvertibleToSingle

This API supports the product infrastructure and is not intended to be used directly from your code. Note   This conversion is not supported. Attempting to do so throws an InvalidCastException.

System_CAPS_pubinterface System_CAPS_privmethod IConvertibleToType

This API supports the product infrastructure and is not intended to be used directly from your code. For a description of this member, see IConvertibleToType.

System_CAPS_pubinterface System_CAPS_privmethod IConvertibleToUInt16

This API supports the product infrastructure and is not intended to be used directly from your code. For a description of this member, see IConvertibleToUInt16.

System_CAPS_pubinterface System_CAPS_privmethod IConvertibleToUInt32

This API supports the product infrastructure and is not intended to be used directly from your code. For a description of this member, see IConvertibleToUInt32.

System_CAPS_pubinterface System_CAPS_privmethod IConvertibleToUInt64

This API supports the product infrastructure and is not intended to be used directly from your code. For a description of this member, see IConvertibleToUInt64.

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.

The following sections examine the relationship between a Char object and a character and discuss some common tasks performed with Char instances.

Char objects, Unicode characters, and strings
Characters and character categories
Characters and text elements
Common operations
Char values and interop

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:
    //       ä
    Imports System.IO
    
    Module Example
       Public Sub Main()
          Dim sw As New StreamWriter("chars1.txt")
          Dim chars() As Char = { ChrW(&h0061), ChrW(&h0308) }
          Dim strng As New String(chars)
          sw.WriteLine(strng) 
          sw.Close()
       End Sub
    End Module
    ' 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
    Imports System.IO
    
    Module Example
       Public Sub Main()
          Dim sw As New StreamWriter(".\chars2.txt")
          Dim utf32 As Integer = &h1D160
          Dim surrogate As String = Char.ConvertFromUtf32(utf32)
          sw.WriteLine("U+{0:X6} UTF-32 = {1} ({2}) UTF-16", 
                       utf32, surrogate, ShowCodePoints(surrogate))
          sw.Close()                    
       End Sub
    
       Private Function ShowCodePoints(value As String) As String
          Dim retval As String = Nothing
          For Each ch In value
             retval += String.Format("U+{0:X4} ", Convert.ToUInt16(ch))
          Next
          Return retval.Trim()
       End Function
    End Module
    ' The example produces the following output:
    '       U+01D160 UTF-32 = ð (U+D834 U+DD60) UTF-16

Each Unicode character or valid surrogate pair belongs to a Unicode category. In the .NET Framework, Unicode categories are represented by members of the UnicodeCategory enumeration and include values such as UnicodeCategoryCurrencySymbol, UnicodeCategoryLowercaseLetter, and UnicodeCategorySpaceSeparator, for example.

To determine the Unicode category of a character, you call the GetUnicodeCategory method. For example, the following example calls the GetUnicodeCategory to display the Unicode category of each character in a string.

using System;
using System.Globalization;

class Example
{
   public static void Main()
   {
      // Define a string with a variety of character categories.
      String s = "The car drove down the narrow, secluded road.";
      // Determine the category of each character.
      foreach (var ch in s)
         Console.WriteLine("'{0}': {1}", ch, Char.GetUnicodeCategory(ch)); 

   }
}
// The example displays the following output:
//      'T': UppercaseLetter
//      'h': LowercaseLetter
//      'e': LowercaseLetter
//      ' ': SpaceSeparator
//      'r': LowercaseLetter
//      'e': LowercaseLetter
//      'd': LowercaseLetter
//      ' ': SpaceSeparator
//      'c': LowercaseLetter
//      'a': LowercaseLetter
//      'r': LowercaseLetter
//      ' ': SpaceSeparator
//      'd': LowercaseLetter
//      'r': LowercaseLetter
//      'o': LowercaseLetter
//      'v': LowercaseLetter
//      'e': LowercaseLetter
//      ' ': SpaceSeparator
//      'd': LowercaseLetter
//      'o': LowercaseLetter
//      'w': LowercaseLetter
//      'n': LowercaseLetter
//      ' ': SpaceSeparator
//      't': LowercaseLetter
//      'h': LowercaseLetter
//      'e': LowercaseLetter
//      ' ': SpaceSeparator
//      'l': LowercaseLetter
//      'o': LowercaseLetter
//      'n': LowercaseLetter
//      'g': LowercaseLetter
//      ',': OtherPunctuation
//      ' ': SpaceSeparator
//      'n': LowercaseLetter
//      'a': LowercaseLetter
//      'r': LowercaseLetter
//      'r': LowercaseLetter
//      'o': LowercaseLetter
//      'w': LowercaseLetter
//      ',': OtherPunctuation
//      ' ': SpaceSeparator
//      's': LowercaseLetter
//      'e': LowercaseLetter
//      'c': LowercaseLetter
//      'l': LowercaseLetter
//      'u': LowercaseLetter
//      'd': LowercaseLetter
//      'e': LowercaseLetter
//      'd': LowercaseLetter
//      ' ': SpaceSeparator
//      'r': LowercaseLetter
//      'o': LowercaseLetter
//      'a': LowercaseLetter
//      'd': LowercaseLetter
//      '.': OtherPunctuation
Imports System.Globalization

Module Example
   Public Sub Main()
      ' Define a string with a variety of character categories.
      Dim s As String = "The car drove down the narrow, secluded road."
      ' Determine the category of each character.
      For Each ch In s
         Console.WriteLine("'{0}': {1}", ch, Char.GetUnicodeCategory(ch)) 
      Next
   End Sub
End Module
' The example displays the following output:
'       'T': UppercaseLetter
'       'h': LowercaseLetter
'       'e': LowercaseLetter
'       ' ': SpaceSeparator
'       'r': LowercaseLetter
'       'e': LowercaseLetter
'       'd': LowercaseLetter
'       ' ': SpaceSeparator
'       'c': LowercaseLetter
'       'a': LowercaseLetter
'       'r': LowercaseLetter
'       ' ': SpaceSeparator
'       'd': LowercaseLetter
'       'r': LowercaseLetter
'       'o': LowercaseLetter
'       'v': LowercaseLetter
'       'e': LowercaseLetter
'       ' ': SpaceSeparator
'       'd': LowercaseLetter
'       'o': LowercaseLetter
'       'w': LowercaseLetter
'       'n': LowercaseLetter
'       ' ': SpaceSeparator
'       't': LowercaseLetter
'       'h': LowercaseLetter
'       'e': LowercaseLetter
'       ' ': SpaceSeparator
'       'l': LowercaseLetter
'       'o': LowercaseLetter
'       'n': LowercaseLetter
'       'g': LowercaseLetter
'       ',': OtherPunctuation
'       ' ': SpaceSeparator
'       'n': LowercaseLetter
'       'a': LowercaseLetter
'       'r': LowercaseLetter
'       'r': LowercaseLetter
'       'o': LowercaseLetter
'       'w': LowercaseLetter
'       ',': OtherPunctuation
'       ' ': SpaceSeparator
'       's': LowercaseLetter
'       'e': LowercaseLetter
'       'c': LowercaseLetter
'       'l': LowercaseLetter
'       'u': LowercaseLetter
'       'd': LowercaseLetter
'       'e': LowercaseLetter
'       'd': LowercaseLetter
'       ' ': SpaceSeparator
'       'r': LowercaseLetter
'       'o': LowercaseLetter
'       'a': LowercaseLetter
'       'd': LowercaseLetter
'       '.': OtherPunctuation

Internally, for characters outside the ASCII range (U+0000 through U+00FF), the GetUnicodeCategory method depends on Unicode categories reported by the CharUnicodeInfo class. Starting with the .NET Framework 4.6.2, Unicode characters are classified based on The Unix Standard, Version 8.0.0. In versions of the .NET Framework from the .NET Framework 4 to the .NET Framework 4.6.1, they are classified based on The Unix Standard, Version 6.3.0.

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.

using System;

public class Example
{
   public static void Main()
   {
      string result = String.Empty;
      for (int ctr = 0x10107; ctr <= 0x10110; ctr++)  // Range of Aegean numbers.
         result += Char.ConvertFromUtf32(ctr);

      Console.WriteLine("The string contains {0} characters.", result.Length); 
   }
}
// The example displays the following output:
//     The string contains 20 characters.
Module Example
   Public Sub Main()
      Dim result As String = String.Empty
      For ctr As Integer = &h10107 To &h10110     ' Range of Aegean numbers.
         result += Char.ConvertFromUtf32(ctr)
      Next         
      Console.WriteLine("The string contains {0} characters.", result.Length) 
   End Sub
End Module
' The example displays the following output:
'     The string contains 20 characters.

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.

    using System;
    using System.Globalization;
    
    public class Example
    {
       public static void Main()
       {
          string result = String.Empty;
          for (int ctr = 0x10107; ctr <= 0x10110; ctr++)  // Range of Aegean numbers.
             result += Char.ConvertFromUtf32(ctr);
    
          StringInfo si = new StringInfo(result);
          Console.WriteLine("The string contains {0} characters.", 
                            si.LengthInTextElements); 
       }
    }
    // The example displays the following output:
    //       The string contains 10 characters.
    Imports System.Globalization
    
    Module Example
       Public Sub Main()
          Dim result As String = String.Empty
          For ctr As Integer = &h10107 To &h10110     ' Range of Aegean numbers.
             result += Char.ConvertFromUtf32(ctr)
          Next         
          Dim si As New StringInfo(result)
          Console.WriteLine("The string contains {0} characters.", si.LengthInTextElements) 
       End Sub
    End Module
    ' The example displays the following output:
    '       The string contains 10 characters.
  • If a string contains a base character that has one or more combining characters, you can call the StringNormalize method to convert the substring to a single UTF-16 encoded code unit. The following example calls the StringNormalize 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).

    using System;
    
    public class Example
    {
       public static void Main()
       {
          string combining = "\u0061\u0308";
          ShowString(combining);
    
          string normalized = combining.Normalize();
          ShowString(normalized);
       }
    
       private static void ShowString(string s)
       {
          Console.Write("Length of string: {0} (", s.Length);
          for (int ctr = 0; ctr < s.Length; ctr++) {
             Console.Write("U+{0:X4}", Convert.ToUInt16(s[ctr]));
             if (ctr != s.Length - 1) Console.Write(" ");
          } 
          Console.WriteLine(")\n");
       }
    }
    // The example displays the following output:
    //       Length of string: 2 (U+0061 U+0308)
    //       
    //       Length of string: 1 (U+00E4)
    Module Example
       Public Sub Main()
          Dim combining As String = ChrW(&h0061) + ChrW(&h0308)
          ShowString(combining)
    
          Dim normalized As String = combining.Normalize()
          ShowString(normalized)
       End Sub
    
       Private Sub ShowString(s As String)
          Console.Write("Length of string: {0} (", s.Length)
          For ctr As Integer = 0 To s.Length - 1
             Console.Write("U+{0:X4}", Convert.ToUInt16(s(ctr)))
             If ctr <> s.Length - 1 Then Console.Write(" ")
          Next 
          Console.WriteLine(")")
          Console.WriteLine()
       End Sub
    End Module
    ' The example displays the following output:
    '       Length of string: 2 (U+0061 U+0308)
    '       
    '       Length of string: 1 (U+00E4)

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

For a single character: ConvertToInt32

For a surrogate pair or a character in a string: CharConvertToUtf32

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

When a managed Char type, which is represented as a Unicode UTF-8 encoded code unit, is passed to unmanaged code, the interop marshaler converts the character set to ANSI. You can apply the DllImportAttribute attribute to platform invoke declarations and the StructLayoutAttribute attribute to a COM interop declaration to control which character set a marshaled Char type uses.

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

Module CharStructure

    Public Sub Main()

        Dim chA As Char
        chA = "A"c
        Dim ch1 As Char
        ch1 = "1"c
        Dim str As String
        str = "test string"

        Console.WriteLine(chA.CompareTo("B"c))          ' Output: "-1" (meaning 'A' is 1 less than 'B')
        Console.WriteLine(chA.Equals("A"c))             ' Output: "True"
        Console.WriteLine(Char.GetNumericValue(ch1))    ' Output: "1"
        Console.WriteLine(Char.IsControl(Chr(9)))       ' Output: "True"
        Console.WriteLine(Char.IsDigit(ch1))            ' Output: "True"
        Console.WriteLine(Char.IsLetter(","c))          ' Output: "False"
        Console.WriteLine(Char.IsLower("u"c))           ' Output: "True"
        Console.WriteLine(Char.IsNumber(ch1))           ' Output: "True"
        Console.WriteLine(Char.IsPunctuation("."c))     ' Output: "True"
        Console.WriteLine(Char.IsSeparator(str, 4))     ' Output: "True"
        Console.WriteLine(Char.IsSymbol("+"c))          ' Output: "True"
        Console.WriteLine(Char.IsWhiteSpace(str, 4))    ' Output: "True"
        Console.WriteLine(Char.Parse("S"))              ' Output: "S"
        Console.WriteLine(Char.ToLower("M"c))           ' Output: "m"
        Console.WriteLine("x"c.ToString())              ' Output: "x"

    End Sub

End Module
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"
}
Universal Windows Platform
Available since 8
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1

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.

Return to top