Version.TryParse 方法  
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
重载
| TryParse(ReadOnlySpan<Byte>, Version) | |
| TryParse(String, Version) | 尝试将版本号的字符串表示形式转换为等效的 Version 对象,并返回一个指示转换是否成功的值。 | 
| TryParse(ReadOnlySpan<Char>, Version) | 尝试将表示版本号的指定字符只读范围转换为等效的 Version 对象,并返回一个指示转换是否成功的值。 | 
TryParse(ReadOnlySpan<Byte>, Version)
- Source:
- Version.cs
public:
 static bool TryParse(ReadOnlySpan<System::Byte> utf8Text, [Runtime::InteropServices::Out] Version ^ % result);public static bool TryParse(ReadOnlySpan<byte> utf8Text, out Version? result);static member TryParse : ReadOnlySpan<byte> * Version -> boolPublic Shared Function TryParse (utf8Text As ReadOnlySpan(Of Byte), ByRef result As Version) As Boolean参数
- utf8Text
- ReadOnlySpan<Byte>
- result
- Version
返回
适用于
TryParse(String, Version)
- Source:
- Version.cs
- Source:
- Version.cs
- Source:
- Version.cs
- Source:
- Version.cs
尝试将版本号的字符串表示形式转换为等效的 Version 对象,并返回一个指示转换是否成功的值。
public:
 static bool TryParse(System::String ^ input, [Runtime::InteropServices::Out] Version ^ % result);public static bool TryParse(string input, out Version result);public static bool TryParse(string? input, out Version? result);static member TryParse : string * Version -> boolPublic Shared Function TryParse (input As String, ByRef result As Version) As Boolean参数
- input
- String
包含要转换的版本号的字符串。
- result
- Version
当此方法返回时,如果转换成功,则包含与 input 中所包含的数字等效的 Version。 如果 input 为 null、Empty 或者如果转换失败,当此方法返回时,result 则为 null。
返回
如果 true 参数成功转换,则为 input;否则为 false。
示例
以下示例使用 TryParse 方法分析包含版本信息的多个字符串。
using System;
public class Example
{
   public static void Main()
   {
      string input = "4.0";
      ParseVersion(input);
      
      input = "4.0.";
      ParseVersion(input);
      
      input = "1.1.2";
      ParseVersion(input);
      
      input = "1.1.2.01702";
      ParseVersion(input);
      
      input = "1.1.2.0702.119";
      ParseVersion(input);
      
      input =  "1.3.5.2150000000";
      ParseVersion(input);
   }
   
   private static void ParseVersion(string input)
   {
      Version ver = null;
      if (Version.TryParse(input, out ver))
         Console.WriteLine("Converted '{0} to {1}.", input, ver);
      else
         Console.WriteLine("Unable to determine the version from '{0}'.",
                           input);
   }
}
// The example displays the following output:
//       Converted '4.0 to 4.0.
//       Unable to determine the version from '4.0.'.
//       Converted '1.1.2 to 1.1.2.
//       Converted '1.1.2.01702 to 1.1.2.1702.
//       Unable to determine the version from '1.1.2.0702.119'.
//       Unable to determine the version from '1.3.5.2150000000'.
open System
let parseVersion (input: string) =
    match Version.TryParse input with
    | true, ver ->
        printfn $"Converted '{input} to {ver}."
    | _ ->
        printfn $"Unable to determine the version from '{input}'."
[<EntryPoint>]
let main _ =
    let input = "4.0"
    parseVersion input
    let input = "4.0."
    parseVersion input
    let input = "1.1.2"
    parseVersion input
    let input = "1.1.2.01702"
    parseVersion input
    let input = "1.1.2.0702.119"
    parseVersion input
    let input =  "1.3.5.2150000000"
    parseVersion input
    0
// The example displays the following output:
//       Converted '4.0 to 4.0.
//       Unable to determine the version from '4.0.'.
//       Converted '1.1.2 to 1.1.2.
//       Converted '1.1.2.01702 to 1.1.2.1702.
//       Unable to determine the version from '1.1.2.0702.119'.
//       Unable to determine the version from '1.3.5.2150000000'.
Module Example
   Public Sub Main()
      Dim input As String = "4.0"
      ParseVersion(input)
      
      input = "4.0."
      ParseVersion(input)
      
      input = "1.1.2"
      ParseVersion(input)
      
      input = "1.1.2.01702"
      ParseVersion(input)
      
      input = "1.1.2.0702.119"
      ParseVersion(input)
      
      input =  "1.3.5.2150000000"
      ParseVersion(input)
   End Sub
   
   Private Sub ParseVersion(input As String)
      Dim ver As Version = Nothing
      If Version.TryParse(input, ver) Then
         Console.WriteLine("Converted '{0} to {1}.", input, ver)
      Else
         Console.WriteLine("Unable to determine the version from '{0}'.",
                           input)
      End If
   End Sub
End Module
' The example displays the following output:
'       Converted '4.0 to 4.0.
'       Unable to determine the version from '4.0.'.
'       Converted '1.1.2 to 1.1.2.
'       Converted '1.1.2.01702 to 1.1.2.1702.
'       Unable to determine the version from '1.1.2.0702.119'.
'       Unable to determine the version from '1.3.5.2150000000'.
注解
方法 TryParse 类似于 方法 Parse ,只是在转换失败时不会引发异常。 相反,如果 是 null,具有少于两个或四个以上的分量、至少有一个不是整数的分量、至少有一个小于零的分量或至少有一个大于 Int32.MaxValue的分量,则返回 falseinput 。
要使分析操作成功, input 参数必须采用以下格式:
major.minor[.build[.revision]]
其中 major、 minor、 build和 revision 是版本号的四个组件的字符串表示形式:主版本号、次要版本号、内部版本号和修订号。 可选组件显示在方括号中, ([ 和 ]) 。 组件必须按顺序显示,并且必须用句点分隔。
另请参阅
适用于
TryParse(ReadOnlySpan<Char>, Version)
- Source:
- Version.cs
- Source:
- Version.cs
- Source:
- Version.cs
- Source:
- Version.cs
尝试将表示版本号的指定字符只读范围转换为等效的 Version 对象,并返回一个指示转换是否成功的值。
public:
 static bool TryParse(ReadOnlySpan<char> input, [Runtime::InteropServices::Out] Version ^ % result);public static bool TryParse(ReadOnlySpan<char> input, out Version? result);public static bool TryParse(ReadOnlySpan<char> input, out Version result);static member TryParse : ReadOnlySpan<char> * Version -> boolPublic Shared Function TryParse (input As ReadOnlySpan(Of Char), ByRef result As Version) As Boolean参数
- input
- ReadOnlySpan<Char>
字符的只读范围,其中包含要转换的版本号。
- result
- Version
当此方法返回时,如果转换成功,则包含与 input 中所包含的数字等效的 Version。 如果 input 为 null、Empty 或者如果转换失败,当此方法返回时,result 则为 null。
返回
如果 true 参数成功转换,则为 input;否则为 false。
注解
方法 TryParse 类似于 方法 Parse ,只是在转换失败时不会引发异常。 相反,如果 是 null,具有少于两个或四个以上的分量、至少有一个不是整数的分量、至少有一个小于零的分量或至少有一个大于 Int32.MaxValue的分量,则返回 falseinput 。
要使分析操作成功, input 参数必须采用以下格式:
major.minor[.build[.revision]]
其中 major、 minor、 build和 revision 是版本号的四个组件的字符串表示形式:主版本号、次要版本号、内部版本号和修订号。 可选组件显示在方括号中, ([ 和 ]) 。 组件必须按顺序显示,并且必须用句点分隔。