Export (0) Print
Expand All

Convert.ToChar Method (String)

Converts the first character of a String to a Unicode character.

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

public static char ToChar(
	string value
)

Parameters

value
Type: System.String
A String of length 1 or null.

Return Value

Type: System.Char
The Unicode character equivalent to the first and only character in value.

ExceptionCondition
ArgumentNullException

value is null.

FormatException

The length of value is not 1.

value must be a string that contains a single character.

If you prefer not to handle an exception if the conversion fails, you can call the Char.TryParse method instead. It returns a Boolean value that indicates whether the conversion succeeded or failed.

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


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


Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Community Additions

ADD
Show:
© 2014 Microsoft