SByte.TryParse 方法  
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
将数字的字符串表示形式转换为其等效的 8 位带符号整数。 返回代码指示转换是成功还是失败。
重载
| TryParse(ReadOnlySpan<Byte>, IFormatProvider, SByte) | 尝试将 UTF-8 字符的范围分析为值。 | 
| TryParse(ReadOnlySpan<Char>, SByte) | 尝试将数字的跨度表示形式转换为其等效 SByte,并返回一个值,该值指示转换是否成功。 | 
| TryParse(String, SByte) | 尝试将数字的字符串表示形式转换为其等效 SByte,并返回一个值,该值指示转换是否成功。 | 
| TryParse(ReadOnlySpan<Char>, IFormatProvider, SByte) | 尝试将字符范围分析为值。 | 
| TryParse(String, IFormatProvider, SByte) | 尝试将字符串分析为值。 | 
| TryParse(ReadOnlySpan<Byte>, NumberStyles, IFormatProvider, SByte) | 尝试将 UTF-8 字符的范围分析为值。 | 
| TryParse(ReadOnlySpan<Byte>, SByte) | 尝试将包含数字字符串表示形式的 UTF-8 字符范围转换为其等效的 8 位带符号整数。 | 
| TryParse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider, SByte) | 尝试将指定样式和区域性特定格式的数字的跨度表示形式转换为其等效 SByte,并返回一个值,该值指示转换是否成功。 | 
| TryParse(String, NumberStyles, IFormatProvider, SByte) | 尝试将指定样式和区域性特定格式的数字的字符串表示形式转换为其等效 SByte,并返回一个值,该值指示转换是否成功。 | 
TryParse(ReadOnlySpan<Byte>, IFormatProvider, SByte)
- Source:
- SByte.cs
- Source:
- SByte.cs
- Source:
- SByte.cs
尝试将 UTF-8 字符的范围分析为值。
public:
 static bool TryParse(ReadOnlySpan<System::Byte> utf8Text, IFormatProvider ^ provider, [Runtime::InteropServices::Out] System::SByte % result) = IUtf8SpanParsable<System::SByte>::TryParse;public static bool TryParse(ReadOnlySpan<byte> utf8Text, IFormatProvider? provider, out sbyte result);static member TryParse : ReadOnlySpan<byte> * IFormatProvider * sbyte -> boolPublic Shared Function TryParse (utf8Text As ReadOnlySpan(Of Byte), provider As IFormatProvider, ByRef result As SByte) As Boolean参数
- utf8Text
- ReadOnlySpan<Byte>
要分析的 UTF-8 字符的范围。
- provider
- IFormatProvider
一个对象,提供有关 utf8Text的区域性特定格式设置信息。
- result
- SByte
返回时,包含成功分析 utf8Text 或失败时未定义值的结果。
返回
如果已成功分析 utf8Text,则 true;否则,false。
适用于
TryParse(ReadOnlySpan<Char>, SByte)
- Source:
- SByte.cs
- Source:
- SByte.cs
- Source:
- SByte.cs
- Source:
- SByte.cs
重要
此 API 不符合 CLS。
尝试将数字的跨度表示形式转换为其等效 SByte,并返回一个值,该值指示转换是否成功。
public:
 static bool TryParse(ReadOnlySpan<char> s, [Runtime::InteropServices::Out] System::SByte % result);public static bool TryParse(ReadOnlySpan<char> s, out sbyte result);[System.CLSCompliant(false)]
public static bool TryParse(ReadOnlySpan<char> s, out sbyte result);static member TryParse : ReadOnlySpan<char> * sbyte -> bool[<System.CLSCompliant(false)>]
static member TryParse : ReadOnlySpan<char> * sbyte -> boolPublic Shared Function TryParse (s As ReadOnlySpan(Of Char), ByRef result As SByte) As Boolean参数
- s
- ReadOnlySpan<Char>
一个范围,包含表示要转换的数字的字符。
- result
- SByte
此方法返回时,包含与转换成功时 s 中包含的数字等效的 8 位有符号整数值;如果转换失败,则为零。 如果 s 参数 null 或 Empty、格式不正确或表示小于 SByte.MinValue 或大于 SByte.MaxValue的数字,则转换失败。 此参数未初始化传递;将覆盖最初在 result 中提供的任何值。
返回
如果已成功转换 s,则 true;否则,false。
- 属性
适用于
TryParse(String, SByte)
- Source:
- SByte.cs
- Source:
- SByte.cs
- Source:
- SByte.cs
- Source:
- SByte.cs
重要
此 API 不符合 CLS。
尝试将数字的字符串表示形式转换为其等效 SByte,并返回一个值,该值指示转换是否成功。
public:
 static bool TryParse(System::String ^ s, [Runtime::InteropServices::Out] System::SByte % result);[System.CLSCompliant(false)]
public static bool TryParse(string s, out sbyte result);public static bool TryParse(string? s, out sbyte result);[System.CLSCompliant(false)]
public static bool TryParse(string? s, out sbyte result);[<System.CLSCompliant(false)>]
static member TryParse : string * sbyte -> boolstatic member TryParse : string * sbyte -> boolPublic Shared Function TryParse (s As String, ByRef result As SByte) As Boolean参数
- s
- String
包含要转换的数字的字符串。
- result
- SByte
此方法返回时,包含与转换成功时 s 中包含的数字等效的 8 位有符号整数值;如果转换失败,则为零。 如果 s 参数 null 或 Empty、格式不正确或表示小于 SByte.MinValue 或大于 SByte.MaxValue的数字,则转换失败。 此参数未初始化传递;将覆盖最初在 result 中提供的任何值。
返回
如果已成功转换 s,则 true;否则,false。
- 属性
示例
以下示例尝试通过调用 TryParse(String, SByte) 方法将数组中的字符串转换为 SByte 值。
string[] numericStrings = {"-3.6", "12.8", "+16.7", "    3   ", "(17)", 
                           "-17", "+12", "18-", "987", "1,024", "  127 "};
sbyte number;
foreach (string numericString in numericStrings)
{
   if (sbyte.TryParse(numericString, out number)) 
      Console.WriteLine("Converted '{0}' to {1}.", numericString, number);
   else
      Console.WriteLine("Cannot convert '{0}' to an SByte.", numericString);
}
// The example displays the following output to the console:
//       Cannot convert '-3.6' to an SByte.
//       Cannot convert '12.8' to an SByte.
//       Cannot convert '+16.7' to an SByte.
//       Converted '    3   ' to 3.
//       Cannot convert '(17)' to an SByte.
//       Converted '-17' to -17.
//       Converted '+12' to 12.
//       Cannot convert '18-' to an SByte.
//       Cannot convert '987' to an SByte.
//       Cannot convert '1,024' to an SByte.
//       Converted '  127 ' to 127.
open System
let numericStrings = 
    [| "-3.6"; "12.8"; "+16.7"; "    3   "; "(17)" 
       "-17"; "+12"; "18-"; "987"; "1,024"; "  127 " |]
for numericString in numericStrings do
    match SByte.TryParse numericString with 
    | true, number ->
        printfn $"Converted '{numericString}' to {number}."
    | _ ->
        printfn $"Cannot convert '{numericString}' to an SByte."
// The example displays the following output to the console:
//       Cannot convert '-3.6' to an SByte.
//       Cannot convert '12.8' to an SByte.
//       Cannot convert '+16.7' to an SByte.
//       Converted '    3   ' to 3.
//       Cannot convert '(17)' to an SByte.
//       Converted '-17' to -17.
//       Converted '+12' to 12.
//       Cannot convert '18-' to an SByte.
//       Cannot convert '987' to an SByte.
//       Cannot convert '1,024' to an SByte.
//       Converted '  127 ' to 127.
Dim numericStrings() As String = {"-3.6", "12.8", "+16.7", "    3   ", _
                                  "(17)", "-17", "+12", "18-", "987", _
                                  "1,024", "  127 "}
Dim number As SByte
For Each numericString As String In numericStrings
   If SByte.TryParse(numericString, number) Then
      Console.WriteLine("Converted '{0}' to {1}.", numericString, number)
   Else
      Console.WriteLine("Cannot convert '{0}' to an SByte.", numericString)
   End If      
Next
' The example displays the following output to the console:
'       Cannot convert '-3.6' to an SByte.
'       Cannot convert '12.8' to an SByte.
'       Cannot convert '+16.7' to an SByte.
'       Converted '    3   ' to 3.
'       Cannot convert '(17)' to an SByte.
'       Converted '-17' to -17.
'       Converted '+12' to 12.
'       Cannot convert '18-' to an SByte.
'       Cannot convert '987' to an SByte.
'       Cannot convert '1,024' to an SByte.
'       Converted '  127 ' to 127.
注解
              SByte.TryParse(String, SByte) 方法类似于 SByte.Parse(String) 方法,只是转换失败时不会引发异常。 如果 value 无效且无法成功分析,则此方法无需使用异常处理来测试 FormatException。
              s 参数应为十进制数的字符串表示形式,格式如下:
[ws][符号]位[ws]
方括号 ([ 和 ]) 中的元素是可选的。 下表描述了每个元素。
| 元素 | 描述 | 
|---|---|
| ws | 可选空格。 | 
| 签名 | 可选符号。 有效符号字符由当前区域性的 NumberFormatInfo.NegativeSign 和 NumberFormatInfo.PositiveSign 属性决定。 | 
| 位 | 从 0 到 9 的十进制数字序列。 | 
注意
由 value 参数指定的字符串不能包含任何组分隔符或小数分隔符,并且不能包含小数部分。
              s 参数使用 NumberStyles.Integer 样式进行解释。 除了十进制数字,还允许使用前导符号的前导和尾随空格。 若要显式定义具有区域性特定的格式设置信息的样式元素,可以在 value中显示,请调用 TryParse(String, NumberStyles, IFormatProvider, SByte) 方法。
              s 参数通过使用当前区域性的 NumberFormatInfo 对象中的格式设置信息进行分析。 有关详细信息,请参阅 NumberFormatInfo.CurrentInfo。
此重载将 value 参数中的所有数字解释为十进制数字。 若要分析十六进制数的字符串表示形式,请改为调用 TryParse(String, NumberStyles, IFormatProvider, SByte) 重载。
另请参阅
适用于
TryParse(ReadOnlySpan<Char>, IFormatProvider, SByte)
- Source:
- SByte.cs
- Source:
- SByte.cs
- Source:
- SByte.cs
- Source:
- SByte.cs
尝试将字符范围分析为值。
public:
 static bool TryParse(ReadOnlySpan<char> s, IFormatProvider ^ provider, [Runtime::InteropServices::Out] System::SByte % result) = ISpanParsable<System::SByte>::TryParse;public static bool TryParse(ReadOnlySpan<char> s, IFormatProvider? provider, out sbyte result);static member TryParse : ReadOnlySpan<char> * IFormatProvider * sbyte -> boolPublic Shared Function TryParse (s As ReadOnlySpan(Of Char), provider As IFormatProvider, ByRef result As SByte) As Boolean参数
- s
- ReadOnlySpan<Char>
要分析的字符范围。
- provider
- IFormatProvider
一个对象,提供有关 s的区域性特定格式设置信息。
- result
- SByte
此方法返回时,包含成功分析 s的结果或失败时未定义的值。
返回
如果已成功分析 s,则 true;否则,false。
适用于
TryParse(String, IFormatProvider, SByte)
- Source:
- SByte.cs
- Source:
- SByte.cs
- Source:
- SByte.cs
- Source:
- SByte.cs
尝试将字符串分析为值。
public:
 static bool TryParse(System::String ^ s, IFormatProvider ^ provider, [Runtime::InteropServices::Out] System::SByte % result) = IParsable<System::SByte>::TryParse;public static bool TryParse(string? s, IFormatProvider? provider, out sbyte result);static member TryParse : string * IFormatProvider * sbyte -> boolPublic Shared Function TryParse (s As String, provider As IFormatProvider, ByRef result As SByte) As Boolean参数
- s
- String
要分析的字符串。
- provider
- IFormatProvider
一个对象,提供有关 s的区域性特定格式设置信息。
- result
- SByte
此方法返回时,包含成功分析 s 或失败时未定义的值的结果。
返回
如果已成功分析 s,则 true;否则,false。
适用于
TryParse(ReadOnlySpan<Byte>, NumberStyles, IFormatProvider, SByte)
- Source:
- SByte.cs
- Source:
- SByte.cs
- Source:
- SByte.cs
尝试将 UTF-8 字符的范围分析为值。
public:
 static bool TryParse(ReadOnlySpan<System::Byte> utf8Text, System::Globalization::NumberStyles style, IFormatProvider ^ provider, [Runtime::InteropServices::Out] System::SByte % result) = System::Numerics::INumberBase<System::SByte>::TryParse;public static bool TryParse(ReadOnlySpan<byte> utf8Text, System.Globalization.NumberStyles style, IFormatProvider? provider, out sbyte result);static member TryParse : ReadOnlySpan<byte> * System.Globalization.NumberStyles * IFormatProvider * sbyte -> boolPublic Shared Function TryParse (utf8Text As ReadOnlySpan(Of Byte), style As NumberStyles, provider As IFormatProvider, ByRef result As SByte) As Boolean参数
- utf8Text
- ReadOnlySpan<Byte>
要分析的 UTF-8 字符的范围。
- style
- NumberStyles
              utf8Text中可以存在的数字样式的按位组合。
- provider
- IFormatProvider
一个对象,提供有关 utf8Text的区域性特定格式设置信息。
- result
- SByte
返回时,包含成功分析 utf8Text 或失败时未定义值的结果。
返回
如果已成功分析 utf8Text,则 true;否则,false。
适用于
TryParse(ReadOnlySpan<Byte>, SByte)
- Source:
- SByte.cs
- Source:
- SByte.cs
- Source:
- SByte.cs
尝试将包含数字字符串表示形式的 UTF-8 字符范围转换为其等效的 8 位带符号整数。
public:
 static bool TryParse(ReadOnlySpan<System::Byte> utf8Text, [Runtime::InteropServices::Out] System::SByte % result);public static bool TryParse(ReadOnlySpan<byte> utf8Text, out sbyte result);static member TryParse : ReadOnlySpan<byte> * sbyte -> boolPublic Shared Function TryParse (utf8Text As ReadOnlySpan(Of Byte), ByRef result As SByte) As Boolean参数
- utf8Text
- ReadOnlySpan<Byte>
一个范围,包含表示要转换的数字的 UTF-8 字符。
- result
- SByte
此方法返回时,包含与转换成功时 utf8Text 中包含的数字等效的 8 位有符号整数值;如果转换失败,则包含零。 此参数未初始化传递;将覆盖最初在结果中提供的任何值。
返回
如果已成功转换 utf8Text,则 true;否则,false。
适用于
TryParse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider, SByte)
- Source:
- SByte.cs
- Source:
- SByte.cs
- Source:
- SByte.cs
- Source:
- SByte.cs
重要
此 API 不符合 CLS。
尝试将指定样式和区域性特定格式的数字的跨度表示形式转换为其等效 SByte,并返回一个值,该值指示转换是否成功。
public:
 static bool TryParse(ReadOnlySpan<char> s, System::Globalization::NumberStyles style, IFormatProvider ^ provider, [Runtime::InteropServices::Out] System::SByte % result) = System::Numerics::INumberBase<System::SByte>::TryParse;public:
 static bool TryParse(ReadOnlySpan<char> s, System::Globalization::NumberStyles style, IFormatProvider ^ provider, [Runtime::InteropServices::Out] System::SByte % result);public static bool TryParse(ReadOnlySpan<char> s, System.Globalization.NumberStyles style, IFormatProvider? provider, out sbyte result);[System.CLSCompliant(false)]
public static bool TryParse(ReadOnlySpan<char> s, System.Globalization.NumberStyles style, IFormatProvider provider, out sbyte result);[System.CLSCompliant(false)]
public static bool TryParse(ReadOnlySpan<char> s, System.Globalization.NumberStyles style, IFormatProvider? provider, out sbyte result);static member TryParse : ReadOnlySpan<char> * System.Globalization.NumberStyles * IFormatProvider * sbyte -> bool[<System.CLSCompliant(false)>]
static member TryParse : ReadOnlySpan<char> * System.Globalization.NumberStyles * IFormatProvider * sbyte -> boolPublic Shared Function TryParse (s As ReadOnlySpan(Of Char), style As NumberStyles, provider As IFormatProvider, ByRef result As SByte) As Boolean参数
- s
- ReadOnlySpan<Char>
一个范围,包含表示要转换的数字的字符。
- style
- NumberStyles
枚举值的按位组合,指示允许的 s格式。 要指定的典型值为 Integer。
- provider
- IFormatProvider
一个对象,提供有关 s的区域性特定格式设置信息。
- result
- SByte
此方法返回时,如果转换成功,则包含与 s中包含的数字等效的 8 位有符号整数值;如果转换失败,则包含零。 如果 s 参数 null 或 Empty,则转换失败,格式不符合 style,或者表示小于 SByte.MinValue 或大于 SByte.MaxValue的数字。 此参数未初始化传递;将覆盖最初在 result 中提供的任何值。
返回
如果已成功转换 s,则 true;否则,false。
- 属性
适用于
TryParse(String, NumberStyles, IFormatProvider, SByte)
- Source:
- SByte.cs
- Source:
- SByte.cs
- Source:
- SByte.cs
- Source:
- SByte.cs
尝试将指定样式和区域性特定格式的数字的字符串表示形式转换为其等效 SByte,并返回一个值,该值指示转换是否成功。
public:
 static bool TryParse(System::String ^ s, System::Globalization::NumberStyles style, IFormatProvider ^ provider, [Runtime::InteropServices::Out] System::SByte % result);public:
 static bool TryParse(System::String ^ s, System::Globalization::NumberStyles style, IFormatProvider ^ provider, [Runtime::InteropServices::Out] System::SByte % result) = System::Numerics::INumberBase<System::SByte>::TryParse;[System.CLSCompliant(false)]
public static bool TryParse(string s, System.Globalization.NumberStyles style, IFormatProvider provider, out sbyte result);public static bool TryParse(string? s, System.Globalization.NumberStyles style, IFormatProvider? provider, out sbyte result);[System.CLSCompliant(false)]
public static bool TryParse(string? s, System.Globalization.NumberStyles style, IFormatProvider? provider, out sbyte result);[<System.CLSCompliant(false)>]
static member TryParse : string * System.Globalization.NumberStyles * IFormatProvider * sbyte -> boolstatic member TryParse : string * System.Globalization.NumberStyles * IFormatProvider * sbyte -> boolPublic Shared Function TryParse (s As String, style As NumberStyles, provider As IFormatProvider, ByRef result As SByte) As Boolean参数
- s
- String
表示要转换的数字的字符串。
- style
- NumberStyles
枚举值的按位组合,指示允许的 s格式。 要指定的典型值为 Integer。
- provider
- IFormatProvider
一个对象,提供有关 s的区域性特定格式设置信息。
- result
- SByte
此方法返回时,如果转换成功,则包含与 s中包含的数字等效的 8 位有符号整数值;如果转换失败,则包含零。 如果 s 参数 null 或 Empty,则转换失败,格式不符合 style,或者表示小于 SByte.MinValue 或大于 SByte.MaxValue的数字。 此参数未初始化传递;将覆盖最初在 result 中提供的任何值。
返回
如果已成功转换 s,则 true;否则,false。
- 属性
例外
示例
以下示例使用多个不同的字符串和 NumberStyles 值调用 TryParse(String, NumberStyles, IFormatProvider, SByte) 方法。
using System;
using System.Globalization;
public class Example
{
   public static void Main()
   {
      string numericString;
      NumberStyles styles;
      
      numericString = "106";
      styles = NumberStyles.Integer;
      CallTryParse(numericString, styles);
      
      numericString = "-106";
      styles = NumberStyles.None;
      CallTryParse(numericString, styles);
      
      numericString = "103.00";
      styles = NumberStyles.Integer | NumberStyles.AllowDecimalPoint;
      CallTryParse(numericString, styles);
      
      numericString = "103.72";
      styles = NumberStyles.Integer | NumberStyles.AllowDecimalPoint;
      CallTryParse(numericString, styles);
      numericString = "10E-01";
      styles = NumberStyles.Integer | NumberStyles.AllowExponent;
      CallTryParse(numericString, styles); 
      
      numericString = "12E-01";
      CallTryParse(numericString, styles);
          
      numericString = "12E01";
      CallTryParse(numericString, styles); 
      
      numericString = "C8";
      CallTryParse(numericString, NumberStyles.HexNumber);
      
      numericString = "0x8C";
      CallTryParse(numericString, NumberStyles.HexNumber);
   }
   
   private static void CallTryParse(string stringToConvert, NumberStyles styles)
   {
      sbyte number;
      bool result = SByte.TryParse(stringToConvert, styles, 
                                   CultureInfo.InvariantCulture, out number);
      if (result)
         Console.WriteLine($"Converted '{stringToConvert}' to {number}.");
      else
         Console.WriteLine($"Attempted conversion of '{stringToConvert}' failed.");
   }
}
// The example displays the following output:
//       Converted '106' to 106.
//       Attempted conversion of '-106' failed.
//       Converted '103.00' to 103.
//       Attempted conversion of '103.72' failed.
//       Converted '10E-01' to 1.
//       Attempted conversion of '12E-01' failed.
//       Converted '12E01' to 120.
//       Converted 'C8' to -56.
//       Attempted conversion of '0x8C' failed.
open System
open System.Globalization
let callTryParse (stringToConvert: string) styles =
    match SByte.TryParse(stringToConvert, styles, CultureInfo.InvariantCulture) with
    | true, number ->
        printfn $"Converted '{stringToConvert}' to {number}."
    | _ ->
        printfn $"Attempted conversion of '{stringToConvert}' failed."
[<EntryPoint>]
let main _ =
    let numericString = "106"
    let styles = NumberStyles.Integer
    callTryParse numericString styles
    
    let numericString = "-106"
    let styles = NumberStyles.None
    callTryParse numericString styles
    
    let numericString = "103.00"
    let styles = NumberStyles.Integer ||| NumberStyles.AllowDecimalPoint
    callTryParse numericString styles
    
    let numericString = "103.72"
    let styles = NumberStyles.Integer ||| NumberStyles.AllowDecimalPoint
    callTryParse numericString styles
    let numericString = "10E-01"
    let styles = NumberStyles.Integer ||| NumberStyles.AllowExponent
    callTryParse numericString styles 
    
    let numericString = "12E-01"
    callTryParse numericString styles
        
    let numericString = "12E01"
    callTryParse numericString styles 
    
    let numericString = "C8"
    callTryParse numericString NumberStyles.HexNumber
    
    let numericString = "0x8C"
    callTryParse numericString NumberStyles.HexNumber
    0
// The example displays the following output:
//       Converted '106' to 106.
//       Attempted conversion of '-106' failed.
//       Converted '103.00' to 103.
//       Attempted conversion of '103.72' failed.
//       Converted '10E-01' to 1.
//       Attempted conversion of '12E-01' failed.
//       Converted '12E01' to 120.
//       Converted 'C8' to -56.
//       Attempted conversion of '0x8C' failed.
Imports System.Globalization
Module StringParsing
   Public Sub Main()
      Dim numericString As String
      Dim styles As NumberStyles
      
      numericString = "106"
      styles = NumberStyles.Integer
      CallTryParse(numericString, styles)
      
      numericString = "-106"
      styles = NumberStyles.None
      CallTryParse(numericString, styles)
      
      numericString = "103.00"
      styles = NumberStyles.Integer Or NumberStyles.AllowDecimalPoint
      CallTryParse(numericString, styles)
      
      numericString = "103.72"
      styles = NumberStyles.Integer Or NumberStyles.AllowDecimalPoint
      CallTryParse(numericString, styles)
      numericString = "10E-01"
      styles = NumberStyles.Integer Or NumberStyles.AllowExponent
      CallTryParse(numericString, styles) 
      
      numericString = "12E-01"
      CallTryParse(numericString, styles)
          
      numericString = "12E01"
      CallTryParse(numericString, styles) 
      
      numericString = "C8"
      CallTryParse(numericString, NumberStyles.HexNumber)
      
      numericString = "0x8C"
      CallTryParse(numericString, NumberStyles.HexNumber)
   End Sub
   
   Private Sub CallTryParse(stringToConvert As String, styles AS NumberStyles)
      Dim number As SByte
      Dim result As Boolean = SByte.TryParse(stringToConvert, styles, _
                                             CultureInfo.InvariantCulture, number)
      If result Then
         Console.WriteLine("Converted '{0}' to {1}.", stringToConvert, number)
      Else
         Console.WriteLine("Attempted conversion of '{0}' failed.", _
                           Convert.ToString(stringToConvert))
      End If                                                                           
   End Sub
End Module
' The example displays the following output to the console:
'       Converted '106' to 106.
'       Attempted conversion of '-106' failed.
'       Converted '103.00' to 103.
'       Attempted conversion of '103.72' failed.
'       Converted '10E-01' to 1.
'       Attempted conversion of '12E-01' failed.
'       Converted '12E01' to 120.
'       Converted 'C8' to -56.
'       Attempted conversion of '0x8C' failed.
注解
              TryParse(String, NumberStyles, IFormatProvider, SByte) 方法类似于 Parse(String, NumberStyles, IFormatProvider) 方法,只是转换失败时不会引发异常。 如果 value 无效且无法成功分析,则此方法无需使用异常处理来测试 FormatException。
              style 参数定义允许 value 参数中允许的样式元素(如空格或正号或负号),以便分析操作成功。 它必须是 NumberStyles 枚举中的位标志的组合。 根据 style的值,value 参数可能包括以下元素:
[ws][$][符号][数字,]位[.fractional_digits][E[符号]exponential_digits][ws]
如果 style 参数包括 AllowHexSpecifier,value 参数可能包含以下元素:
[ws]hexdigits[ws]
方括号 ([ 和 ]) 中的元素是可选的。 下表描述了每个元素。
| 元素 | 描述 | 
|---|---|
| ws | 可选空格。 如果 style包含 NumberStyles.AllowLeadingWhite 标志,或value末尾(如果style包含 NumberStyles.AllowTrailingWhite 标志,则空格可能会出现在value的开头。 | 
| $ | 区域性特定的货币符号。 字符串中的位置由 provider参数的 GetFormat 方法返回的 NumberFormatInfo 对象的 CurrencyPositivePattern 属性定义。 如果style包含 NumberStyles.AllowCurrencySymbol 标志,货币符号可以出现在value中。 | 
| 签名 | 可选符号。 如果 style包含 NumberStyles.AllowLeadingSign 标志,则符号可以在value开始时显示,如果style包含 NumberStyles.AllowTrailingSign 标志,则它将显示在value末尾。 如果style包含 NumberStyles.AllowParentheses 标志,则可以在value中使用括号来指示负值。 | 
| 位 | 从 0 到 9 的数字序列。 | 
| 、 | 区域性特定的组分隔符。 如果 style包含 NumberStyles.AllowThousands 标志,则由provider指定的区域性的组分隔符可以显示在value中。 | 
| 。 | 区域性特定的小数点符号。 如果 style包含 NumberStyles.AllowDecimalPoint 标志,则由provider指定的区域性的小数点符号可以出现在value中。 | 
| fractional_digits | 数字 0 的一个或多个匹配项。 仅当 style包含 NumberStyles.AllowDecimalPoint 标志时,小数位数才会显示在value中。 | 
| E | “e”或“E”字符,指示该值以指数(科学)表示法表示。 如果 style包含 NumberStyles.AllowExponent 标志,value参数可以表示指数表示法中的数字。 | 
| exponential_digits | 从 0 到 9 的数字序列。 如果 style包含 NumberStyles.AllowExponent 标志,value参数可以表示指数表示法中的数字。 | 
| hexdigits | 从 0 到 f 或 0 到 F 的十六进制数字序列。 | 
注意
无论 style 参数的值如何,分析操作都会忽略 s 中任何终止的 NUL (U+0000) 字符。
仅包含十进制数字(对应于 NumberStyles.None 标志)的字符串始终成功分析。 其余大部分 NumberStyles 成员控制可能存在的元素,但不需要存在于此输入字符串中。 下表指示单个 NumberStyles 成员如何影响 value中可能存在的元素。
| 非复合 NumberStyles值 | 除数字外,值允许的元素 | 
|---|---|
| None | 仅十进制数字。 | 
| AllowDecimalPoint | 小数点(.)和 fractional_digits 元素。 但是,fractional_digits 必须仅包含一个或多个数字,或者该方法返回 false。 | 
| AllowExponent | 指示指数表示法的“e”或“E”字符以及 exponential_digits。 如果 value表示指数表示法中的数字,则它不能具有非零的小数分量。 | 
| AllowLeadingWhite | value开头的 ws 元素。 | 
| AllowTrailingWhite | value末尾的 ws 元素。 | 
| AllowLeadingSign | 符号 元素,然后 数字。 | 
| AllowTrailingSign | 位后,符号 元素。 | 
| AllowParentheses | 符号 元素,以括住数值的括号形式。 | 
| AllowThousands | 组分隔符(,)元素。 | 
| AllowCurrencySymbol | 货币 ($) 元素。 | 
| Currency | 所有元素。 但是, value不能表示十六进制数或指数表示法中的数字。 | 
| Float | ws 元素位于 value开头或末尾,符号value开头,以及小数点(.)符号。value参数也可以使用指数表示法。 | 
| Number | ws、符号、组分隔符(、)和小数点(.)元素。 | 
| Any | 所有元素。 但是, value不能表示十六进制数。 | 
如果使用 NumberStyles.AllowHexSpecifier 标志,value 必须是十六进制值。 有效的十六进制数字为 0-9、a-f 和 A-F。 
              style 中唯一可以存在的其他标志是 NumberStyles.AllowLeadingWhite 和 NumberStyles.AllowTrailingWhite。 (NumberStyles 枚举具有包含两个空格标志的复合样式 HexNumber。
注意
如果 value 是十六进制数的字符串表示形式,则任何修饰(如 0x 或 &h)都不能将其区分为十六进制数。 这会导致转换失败。
              provider 参数是 IFormatProvider 实现。 其 GetFormat 方法返回一个 NumberFormatInfo 对象,该对象提供有关 value格式的区域性特定信息。 
              provider 参数可以是以下任一参数:
- 一个 CultureInfo 对象,该对象表示提供格式设置信息的区域性。 其 GetFormat 方法返回为该区域性提供数值格式信息的 NumberFormatInfo 对象。 
- 提供数字格式信息的 NumberFormatInfo 对象。 (它的 GetFormat 实现只是返回自己。 
- 实现 IFormatProvider的自定义对象。 其 GetFormat 方法实例化并返回提供格式信息的 NumberFormatInfo 对象。 
如果 providernull,则使用当前区域性的 NumberFormatInfo 对象。