System.Convert.ToChar 方法 (String, IFormatProvider)

方法描述

使用指定的区域性特定格式设置信息,将指定字符串的第一个字符转换为 Unicode 字符。

语法定义(C# System.Convert.ToChar 方法 (String, IFormatProvider) 的用法)

public static char ToChar(
	string value,
	IFormatProvider provider
)

参数/返回值

参数值/返回值 参数类型/返回类型 参数描述/返回描述
value System-String 长度为 1 或 null 的字符串。
provider System-IFormatProvider 一个提供区域性特定的格式设置信息的对象。忽略此参数。
返回值 System.Char 与 value 中第一个且仅有的字符等效的 Unicode 字符。

提示和注释

value 必须为包含单个字符的字符串。

如果您希望在转换失败时不处理异常,则可以转而调用 Char.TryParse 方法。 它将返回 Boolean 值,该值指示转换是否成功。

System.Convert.ToChar 方法 (String, IFormatProvider)例子

该示例显示未引用格式提供程序。

using System;
using System.Globalization;

public class DummyProvider : IFormatProvider
{
    // 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 );

        Console.Write( "{0,-20}", argStr );
        return null;
    }
}

class ConvertNonNumericProviderDemo
{
    public static void Main( )
    {
        // Create an instance of IFormatProvider.
        DummyProvider provider = new 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( 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( 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           $                $
*/

异常

异常 异常描述
ArgumentNullException value 为 null。
FormatException value 的长度不是 1。

命名空间

namespace: System

程序集: mscorlib(在 mscorlib.dll 中)

版本信息

.NET Framework 受以下版本支持:4、3.5、3.0、2.0、1.1、1.0 .NET Framework Client Profile 受以下版本支持:4、3.5 SP1 受以下版本支持:

适用平台

Windows 7, Windows Vista SP1 或更高版本, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008(不支持服务器核心), Windows Server 2008 R2(支持 SP1 或更高版本的服务器核心), Windows Server 2003 SP2 .NET Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求。