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

方法描述

使用指定的区域性特定格式设置信息,将数字的指定字符串表示形式转换为等效的 8 位带符号整数。

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

[CLSCompliantAttribute(false)]
public static sbyte ToSByte(
	string value,
	IFormatProvider provider
)

参数/返回值

参数值/返回值 参数类型/返回类型 参数描述/返回描述
value System-String 包含要转换的数字的字符串。
provider System-IFormatProvider 一个提供区域性特定的格式设置信息的对象。
返回值 System.SByte 与 value 等效的 8 位带符号整数。

提示和注释

provider 是获取 NumberFormatInfo 对象的 IFormatProvider 实例。 NumberFormatInfo 对象提供有关 value 格式的区域性特定信息。 如果 provider 为 null,则使用当前区域性的 NumberFormatInfo。

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

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

下面的代码示例使用 ToSByte 方法,通过一个 IFormatProvider 对象转换 SByte 值的字符串表示形式。

// Example of the Convert.ToSByte( string ) and 
// Convert.ToSByte( string, IFormatProvider ) methods.
using System;
using System.Globalization;

class ToSByteProviderDemo
{
    static string format = "{0,-20}{1,-20}{2}";

     // Get the exception type name; remove the namespace prefix.
    static string GetExceptionType( Exception ex )
    {
        string exceptionType = ex.GetType( ).ToString( );
        return exceptionType.Substring( 
            exceptionType.LastIndexOf( '.' ) + 1 );
    }

    static void ConvertToSByte( string numericStr, 
        IFormatProvider provider )
    {
        object defaultValue;
        object providerValue;

        // Convert numericStr to SByte without a format provider.
        try
        {
            defaultValue = Convert.ToSByte( numericStr );
        }
        catch( Exception ex )
        {
            defaultValue = GetExceptionType( ex );
        }

        // Convert numericStr to SByte with a format provider.
        try
        {
            providerValue = Convert.ToSByte( numericStr, provider );
        }
        catch( Exception ex )
        {
            providerValue = GetExceptionType( ex );
        }

        Console.WriteLine( format, numericStr, 
            defaultValue, providerValue );
    }

    public static void Main( )
    {
        // Create a NumberFormatInfo object and set several of its
        // properties that apply to numbers.
        NumberFormatInfo provider = new NumberFormatInfo();

        // These properties affect the conversion.
        provider.NegativeSign = "neg ";
        provider.PositiveSign = "pos ";

        // These properties do not affect the conversion.
        // The input string cannot have decimal and group separators.
        provider.NumberDecimalSeparator = ".";
        provider.NumberNegativePattern = 0;

        Console.WriteLine("This example of\n" +
            "  Convert.ToSByte( string ) and \n" +
            "  Convert.ToSByte( string, IFormatProvider ) " +
            "\ngenerates the following output. It converts " +
            "several strings to \nSByte values, using " +
            "default formatting or a NumberFormatInfo object.\n" );
        Console.WriteLine( format, "String to convert", 
            "Default/exception", "Provider/exception" );
        Console.WriteLine( format, "-----------------", 
            "-----------------", "------------------" );

        // Convert strings, with and without an IFormatProvider.
        ConvertToSByte( "123", provider );
        ConvertToSByte( "+123", provider );
        ConvertToSByte( "pos 123", provider );
        ConvertToSByte( "-123", provider );
        ConvertToSByte( "neg 123", provider );
        ConvertToSByte( "123.", provider );
        ConvertToSByte( "(123)", provider );
        ConvertToSByte( "128", provider );
        ConvertToSByte( "-129", provider );
    }
}

/*
This example of
  Convert.ToSByte( string ) and
  Convert.ToSByte( string, IFormatProvider )
generates the following output. It converts several strings to
SByte values, using default formatting or a NumberFormatInfo object.

String to convert   Default/exception   Provider/exception
-----------------   -----------------   ------------------
123                 123                 123
+123                123                 FormatException
pos 123             FormatException     123
-123                -123                FormatException
neg 123             FormatException     -123
123.                FormatException     FormatException
(123)               FormatException     FormatException
128                 OverflowException   OverflowException
-129                OverflowException   FormatException
*/

异常

异常 异常描述
ArgumentNullException value 为 null。
FormatException value不是由一个可选符号后跟数字序列(0 到 9)组成的。
OverflowException value 表示小于 SByte.MinValue 或大于 SByte.MaxValue 的数字。

命名空间

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 系统要求。