Partager via


Convert.ToChar Method (String, IFormatProvider)

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Converts the first character of a String to a Unicode character using specified culture-specific formatting information.

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

Syntax

'Declaration
Public Shared Function ToChar ( _
    value As String, _
    provider As IFormatProvider _
) As Char
public static char ToChar(
    string value,
    IFormatProvider provider
)

Parameters

  • value
    Type: System.String
    A String of length 1 or nulla null reference (Nothing in Visual Basic).

Return Value

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

Exceptions

Exception Condition
ArgumentNullException

value is nulla null reference (Nothing in Visual Basic).

FormatException

The length of value is not 1.

Remarks

value must be a string that contains a single character.

provider is ignored; it does not participate in this operation.

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.

Examples

The following code example converts a String representation of a Char value with the ToChar method, using an IFormatProvider object that displays the type of the format provider for which it is called. The example shows that the format provider is not referenced.

' Example of selected Convert.ToXXX( String, IFormatProvider ) methods.
Imports System.Globalization

Public Class DummyProvider
   Implements IFormatProvider

   ' Normally, GetFormat returns an object of the requested type
   ' (usually itself) if it is able; otherwise, it returns Nothing. 
   Public Function GetFormat(ByVal argType As Type) As Object _
       Implements IFormatProvider.GetFormat

      ' Here, GetFormat displays the name of argType, after removing 
      ' the namespace information. GetFormat always returns Nothing.
      Dim argStr As String = argType.ToString()
      If argStr = "" Then argStr = "Empty"
      argStr = argStr.Substring(argStr.LastIndexOf("."c) + 1)

      outputBlock.Text &= String.Format("{0,-20}", argStr)
      Return Nothing

   End Function
End Class

Module Example

   Public Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)

      ' Create an instance of IFormatProvider.
      Dim provider As New DummyProvider()
      Dim format As String = "{0,-17}{1,-17}{2}"

      ' Convert these values using DummyProvider.
      Dim Int32A As String = "-252645135"
      Dim DoubleA As String = "61680.3855"
      Dim DayTimeA As String = "2001/9/11 13:45"

      Dim BoolA As String = "True"
      Dim StringA As String = "Qwerty"
      Dim CharA As String = "$"

      outputBlock.Text &= "This example of selected " & _
          "Convert.ToXXX( String, IFormatProvider ) " & vbCrLf & _
          "methods generates the following output. The example " & _
          "displays the " & vbCrLf & "provider type if the " & _
          "IFormatProvider is called." & vbCrLf
      outputBlock.Text &= vbCrLf & _
          "Note: For the ToBoolean, ToString, and ToChar " & _
          "methods, the " & vbCrLf & "IFormatProvider object " & _
          "is not referenced." & vbCrLf

      ' The format provider is called for the following conversions.
      outputBlock.Text &= vbCrLf
      outputBlock.Text &= String.Format(format, "ToInt32", Int32A, _
          Convert.ToInt32(Int32A, provider)) & vbCrLf
      outputBlock.Text &= String.Format(format, "ToDouble", DoubleA, _
          Convert.ToDouble(DoubleA, provider)) & vbCrLf
      outputBlock.Text &= String.Format(format, "ToDateTime", DayTimeA, _
          Convert.ToDateTime(DayTimeA, provider)) & vbCrLf

      ' The format provider is not called for these conversions.
      outputBlock.Text &= vbCrLf
      outputBlock.Text &= String.Format(format, "ToBoolean", BoolA, _
          Convert.ToBoolean(BoolA, provider)) & vbCrLf
      outputBlock.Text &= String.Format(format, "ToString", StringA, _
          Convert.ToString(StringA, provider)) & vbCrLf
      outputBlock.Text &= String.Format(format, "ToChar", CharA, _
          Convert.ToChar(CharA, provider)) & vbCrLf

   End Sub
End Module

' This example of selected Convert.ToXXX( String, IFormatProvider )
' methods generates the following output. The example displays the
' provider type if the IFormatProvider is called.
'
' Note: For the ToBoolean, ToString, and ToChar methods, the
' IFormatProvider object is not referenced.
' 
' NumberFormatInfo    ToInt32          -252645135       -252645135
' NumberFormatInfo    ToDouble         61680.3855       61680.3855
' DateTimeFormatInfo  ToDateTime       2001/9/11 13:45  9/11/2001 1:45:00 PM
' 
' ToBoolean        True             True
' ToString         Qwerty           Qwerty
' ToChar           $                $
// Example of selected Convert.ToXXX( String, IFormatProvider ) methods.
using System;
using System.Globalization;

public class DummyProvider : IFormatProvider
{
   private System.Windows.Controls.TextBlock outputBlock;

   public DummyProvider(System.Windows.Controls.TextBlock outputBlock)
   {
      this.outputBlock = outputBlock;
   }

   // Normally, GetFormat returns an object of the requested type
   // (usually itself) if it is able; otherwise, it returns Nothing. 
   public object GetFormat(Type argType)
   {
      // Here, GetFormat displays the name of argType, after removing 
      // the namespace information. GetFormat always returns null.
      string argStr = argType.ToString();
      if (argStr == "")
         argStr = "Empty";
      argStr = argStr.Substring(argStr.LastIndexOf('.') + 1);

      outputBlock.Text += String.Format("{0,-20}", argStr);
      return null;
   }
}

class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      // Create an instance of IFormatProvider.
      DummyProvider provider = new DummyProvider(outputBlock);
      string format = "{0,-17}{1,-17}{2}";

      // Convert these values using DummyProvider.
      string Int32A = "-252645135";
      string DoubleA = "61680.3855";
      string DayTimeA = "2001/9/11 13:45";

      string BoolA = "True";
      string StringA = "Qwerty";
      string CharA = "$";

      outputBlock.Text += "This example of selected " +
          "Convert.ToXXX( String, IFormatProvider ) \nmethods " +
          "generates the following output. The example displays " +
          "the \nprovider type if the IFormatProvider is called." + "\n";
      outputBlock.Text += "\nNote: For the " +
          "ToBoolean, ToString, and ToChar methods, the \n" +
          "IFormatProvider object is not referenced." + "\n";

      // The format provider is called for the following conversions.
      outputBlock.Text += "\n";
      outputBlock.Text += String.Format(format, "ToInt32", Int32A,
          Convert.ToInt32(Int32A, provider)) + "\n";
      outputBlock.Text += String.Format(format, "ToDouble", DoubleA,
          Convert.ToDouble(DoubleA, provider)) + "\n";
      outputBlock.Text += String.Format(format, "ToDateTime", DayTimeA,
          Convert.ToDateTime(DayTimeA, provider)) + "\n";

      // The format provider is not called for these conversions.
      outputBlock.Text += "\n";
      outputBlock.Text += String.Format(format, "ToBoolean", BoolA,
          Convert.ToBoolean(BoolA, provider)) + "\n";
      outputBlock.Text += String.Format(format, "ToString", StringA,
          Convert.ToString(StringA, provider)) + "\n";
      outputBlock.Text += String.Format(format, "ToChar", CharA,
          Convert.ToChar(CharA, provider)) + "\n";
   }
}

/*
This example of selected Convert.ToXXX( String, IFormatProvider )
methods generates the following output. The example displays the
provider type if the IFormatProvider is called.

Note: For the ToBoolean, ToString, and ToChar methods, the
IFormatProvider object is not referenced.

NumberFormatInfo    ToInt32          -252645135       -252645135
NumberFormatInfo    ToDouble         61680.3855       61680.3855
DateTimeFormatInfo  ToDateTime       2001/9/11 13:45  9/11/2001 1:45:00 PM

ToBoolean        True             True
ToString         Qwerty           Qwerty
ToChar           $                $
*/

Version Information

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

Platforms

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