Export (0) Print
Expand All

Char Structure

Represents a Unicode character.

For a list of all members of this type, see Char Members.

System.Object
   System.ValueType
      System.Char

[Visual Basic]
<Serializable>
Public Structure Char
   Implements IComparable, IConvertible
[C#]
[Serializable]
public struct Char : IComparable, IConvertible
[C++]
[Serializable]
public __value struct Char : public IComparable, IConvertible

[JScript] In JScript, you can use the structures in the .NET Framework, but you cannot define your own.

Thread Safety

This type is safe for multithreaded operations.

Remarks

The Char value type represents a Unicode character, also called a Unicode code point, and is implemented as a 16-bit number ranging in value from hexadecimal 0x0000 to 0xFFFF. A single Char cannot represent a Unicode character that is encoded as a surrogate pair. However, a String, which is a collection of Char objects, can represent a Unicode character encoded as a surrogate pair.

For more information about Unicode, see the Unicode Standard at www.unicode.org .

Char provides methods to compare instances of this type, convert the value of an instance to its string representation, convert the string representation of a number to an instance of this type, and determine whether an instance is in a category such as digit, letter, punctuation, control character, and so on. For information about how format specification codes control the string representation of value types, see Formatting Overview.

As a general principle, class members in the .NET Framework that take or return a Char operate on the numeric (ordinal) value of the Char. Class members that take or return a String operate on the linguistic value of the string, including handling surrogate pairs. For this reason, a Char is not necessarily equivalent to an individual character in a String.

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

Example

[Visual Basic, C#, C++] The following sample demonstrates some of the methods in Char.

[Visual Basic] 
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

[C#] 
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"
    }
}

[C++] 
#using <mscorlib.dll>
using namespace System;

int main() {
   char chA = 'A';
   char ch1 = '1';
   String* str = "test string"; 

   Console::WriteLine(chA.CompareTo(__box('B'))); // Output: "-1" (meaning 'A' is 1 less than 'B')
   Console::WriteLine(chA.Equals(__box('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(__box('x'));                // Output: "x"
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

Assembly: Mscorlib (in Mscorlib.dll)

See Also

Char Members | System Namespace | IComparable | IConvertible | String

Show:
© 2014 Microsoft