Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
ToChar Method (String, IFormatProvider)

Convert::ToChar Method (String, IFormatProvider)

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

Namespace:  System
Assemblies:   System.Runtime.Extensions (in System.Runtime.Extensions.dll)
  mscorlib (in mscorlib.dll)

public:
static wchar_t ToChar(
	String^ value, 
	IFormatProvider^ provider
)

Parameters

value
Type: System::String

A string of length 1 or nullptr.

provider
Type: System::IFormatProvider

An object that supplies culture-specific formatting information. This parameter is ignored.

Return Value

Type: System::Char
A Unicode character that is equivalent to the first and only character in value.

ExceptionCondition
ArgumentNullException

value is nullptr.

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 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.

using namespace System;
using namespace System::Globalization;
ref class DummyProvider: public IFormatProvider
{
public:

   // Normally, GetFormat returns an object of the requested type 
   // (usually itself) if it is able; otherwise, it returns Nothing.  
   virtual 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->Equals( "" ) )
            argStr = "Empty";

      argStr = argStr->Substring( argStr->LastIndexOf( '.' ) + 1 );
      Console::Write( "{0,-20}", argStr );
      return (Object^)0;
   }
};

int main()
{
   // Create an instance of IFormatProvider.
   DummyProvider^ provider = gcnew DummyProvider;
   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 = "$";
   Console::WriteLine( "This example of selected " 
   "Convert::To<Type>( String*, IFormatProvider* ) \nmethods " 
   "generates the following output. The example displays the " 
   "\nprovider type if the IFormatProvider is called." );
   Console::WriteLine( "\nNote: For the " 
   "ToBoolean, ToString, and ToChar methods, the \n" 
   "IFormatProvider object is not referenced." );

   // The format provider is called for the following conversions.
   Console::WriteLine();
   Console::WriteLine( format, "ToInt32", Int32A, Convert::ToInt32( Int32A, provider ) );
   Console::WriteLine( format, "ToDouble", DoubleA, Convert::ToDouble( DoubleA, provider ) );
   Console::WriteLine( format, "ToDateTime", DayTimeA, Convert::ToDateTime( DayTimeA, provider ) );

   // The format provider is not called for these conversions.
   Console::WriteLine();
   Console::WriteLine( format, "ToBoolean", BoolA, Convert::ToBoolean( BoolA, provider ) );
   Console::WriteLine( format, "ToString", StringA, Convert::ToString( StringA, provider ) );
   Console::WriteLine( format, "ToChar", CharA, Convert::ToChar( CharA, provider ) );
}

/*
This example of selected Convert::To<Type>( 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           $                $
*/

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

XNA Framework

Supported in: 3.0, 2.0, 1.0

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone Silverlight 8.1, Windows Phone Silverlight 8

Portable Class Library

Supported in: Portable Class Library
Show:
© 2015 Microsoft