Convert.ToChar Method (Object)

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

Converts the value of the specified Object to a Unicode character.

Namespace: System
Assembly: mscorlib (in mscorlib.dll)

Syntax

Public Shared Function ToChar ( _
    value As Object _
) As Char
public static char ToChar(
    Object value
)

Parameters

Return Value

Type: System..::.Char
The Unicode character equivalent to the value of value.
-or-
Char..::.MinValue if value equals nullNothingnullptra null reference (Nothing in Visual Basic).

Exceptions

Exception Condition
InvalidCastException

value does not implement the IConvertible interface.

Remarks

If value is not nullNothingnullptra null reference (Nothing in Visual Basic), this method wraps a call to the IConvertible..::.ToChar implementation of the underlying type of value.

Examples

The following code sample illustrates the use of ToByte, by attempting to convert a String value to a Char :

Public Sub ConvertStringChar(ByVal stringVal As String)
   Dim charVal As Char = "a"c

   ' A string must be one character long to convert to char.
   Try
      charVal = System.Convert.ToChar(stringVal)
      outputBlock.Text &= String.Format("{0} as a char is {1}", _
                                stringVal, charVal) & vbCrLf
   Catch exception As System.FormatException
      outputBlock.Text &= String.Format( _
       "The string is longer than one character.") & vbCrLf
   Catch exception As System.ArgumentNullException
      outputBlock.Text &= "The string is null." & vbCrLf
   End Try

   ' A char to string conversion will always succeed.
   stringVal = System.Convert.ToString(charVal)
   outputBlock.Text &= String.Format("The character as a string is {0}", _
                             stringVal) & vbCrLf
End Sub
public void ConvertStringChar(string stringVal)
{
   char charVal = 'a';

   // A string must be one character long to convert to char.
   try
   {
      charVal = System.Convert.ToChar(stringVal);
      outputBlock.Text += String.Format("{0} as a char is {1}",
         stringVal, charVal) + "\n";
   }
   catch (System.FormatException)
   {
      outputBlock.Text += String.Format(
         "The string is longer than one character.") + "\n";
   }
   catch (System.ArgumentNullException)
   {
      outputBlock.Text += "The string is null." + "\n";
   }

   // A char to string conversion will always succeed.
   stringVal = System.Convert.ToString(charVal);
   outputBlock.Text += String.Format("The character as a string is {0}",
         stringVal) + "\n";
}

Version Information

Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Platforms

Windows Phone

See Also

Reference

Convert Class

ToChar Overload

System Namespace