Char Data Type (Visual Basic)

Holds unsigned 16-bit (2-byte) code points ranging in value from 0 through 65535. Each code point, or character code, represents a single Unicode character.

Remarks

Use the Char data type when you need to hold only a single character and do not need the overhead of String. In some cases you can use Char(), an array of Char elements, to hold multiple characters.

The default value of Char is the character with a code point of 0.

Unicode Characters

The first 128 code points (0–127) of Unicode correspond to the letters and symbols on a standard U.S. keyboard. These first 128 code points are the same as those the ASCII character set defines. The second 128 code points (128–255) represent special characters, such as Latin-based alphabet letters, accents, currency symbols, and fractions. Unicode uses the remaining code points (256-65535) for a wide variety of symbols, including worldwide textual characters, diacritics, and mathematical and technical symbols.

You can use methods like IsDigit and IsPunctuation on a Char variable to determine its Unicode classification.

Type Conversions

Visual Basic does not convert directly between Char and the numeric types. You can use the Asc or AscW function to convert a Char value to an Integer that represents its code point. You can use the Chr or ChrW function to convert an Integer value to a Char that has that code point.

If the type checking switch (the Option Strict Statement) is on, you must append the literal type character to a single-character string literal to identify it as the Char data type. The following example illustrates this. The first assignment to the charVar variable generates compiler error BC30512 because Option Strict is on. The second compiles successfully because the c literal type character identifies the literal as a Char value.

Option Strict On

Module CharType
    Public Sub Main()
        Dim charVar As Char

        ' This statement generates compiler error BC30512 because Option Strict is On.  
        charVar = "Z"  

        ' The following statement succeeds because it specifies a Char literal.  
        charVar = "Z"c
    End Sub
End Module

Programming Tips

  • Negative Numbers. Char is an unsigned type and cannot represent a negative value. In any case, you should not use Char to hold numeric values.

  • Interop Considerations. If you interface with components not written for the .NET Framework, for example Automation or COM objects, remember that character types have a different data width (8 bits) in other environments. If you pass an 8-bit argument to such a component, declare it as Byte instead of Char in your new Visual Basic code.

  • Widening. The Char data type widens to String. This means you can convert Char to String and will not encounter a System.OverflowException.

  • Type Characters. Appending the literal type character C to a single-character string literal forces it to the Char data type. Char has no identifier type character.

  • Framework Type. The corresponding type in the .NET Framework is the System.Char structure.

See also