Version.TryParse 方法

定義

多載

TryParse(ReadOnlySpan<Char>, Version)

嘗試轉換代表相當於 Version 物件版本號碼的指定唯讀字元延伸,且傳回指出轉換是否成功的值。Tries to convert the specified read-only span of characters representing a version number to an equivalent Version object, and returns a value that indicates whether the conversion succeeded.

TryParse(String, Version)

嘗試將版本號碼的字串表示轉換為對等的 Version 物件,並傳回表示轉換是否成功的值。Tries to convert the string representation of a version number to an equivalent Version object, and returns a value that indicates whether the conversion succeeded.

TryParse(ReadOnlySpan<Char>, Version)

嘗試轉換代表相當於 Version 物件版本號碼的指定唯讀字元延伸,且傳回指出轉換是否成功的值。Tries to convert the specified read-only span of characters representing a version number to an equivalent Version object, and returns a value that indicates whether the conversion succeeded.

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 -> bool
Public Shared Function TryParse (input As ReadOnlySpan(Of Char), ByRef result As Version) As Boolean

參數

input
ReadOnlySpan<Char>

包含欲轉換版本號碼的字元唯讀延伸。A read-only span of characters that contains a version number to convert.

result
Version

當此方法傳回時,為相當於 input 中所包含號碼的 Version (若轉換成功)。When this method returns, the Version equivalent of the number that is contained in input, if the conversion succeeded. 如果 inputnull,則為 Empty;如果轉換失敗,當方法傳回時,result 則為 nullIf input is null, Empty, or if the conversion fails, result is null when the method returns.

傳回

Boolean

如果 true 參數轉換成功,則為 input,否則為 falsetrue if the input parameter was converted successfully; otherwise, false.

備註

TryParse 方法與 Parse 方法類似,不同之處在于它不會在轉換失敗時擲回例外狀況。The TryParse method is similar to the Parse method, except that it doesn't throw an exception if the conversion fails. 相反地,它會傳回 false ,如果 input null、少於兩個或四個以上的元件、至少有一個元件不是整數、至少有一個元件小於零,或至少有一個元件大於 Int32.MaxValueInstead, it returns false if input is null, has fewer than two or more than four components, has at least one component that is not an integer, has at least one component that is less than zero, or has at least one component that is greater than Int32.MaxValue.

若要讓剖析作業成功, input 參數必須採用下列格式:For the parse operation to succeed, the input parameter must be in the following format:

major.minor[.build[.revision]]

其中 majorminorbuildrevision 是版本號碼的四個元件的字串表示:主要版本號碼、次要版本號碼、組建編號和修訂編號。where major, minor, build, and revision are the string representations of the version number's four components: major version number, minor version number, build number, and revision number, respectively. 選擇性元件會以方括弧括住 ( [和] ) 。Optional components are shown in square brackets ([ and ]). 元件必須依序顯示,且必須以句號分隔。The components must appear in order and must be separated by periods.

適用於

產品 Introduced

TryParse(String, Version)

嘗試將版本號碼的字串表示轉換為對等的 Version 物件,並傳回表示轉換是否成功的值。Tries to convert the string representation of a version number to an equivalent Version object, and returns a value that indicates whether the conversion succeeded.

public:
 static bool TryParse(System::String ^ input, [Runtime::InteropServices::Out] Version ^ % result);
public static bool TryParse (string input, out Version result);
static member TryParse : string * Version -> bool
Public Shared Function TryParse (input As String, ByRef result As Version) As Boolean

參數

input
String

字串,包含要轉換的版本號碼。A string that contains a version number to convert.

result
Version

當這個方法傳回時,如果轉換成功,則會包含 Version,相當於 input 所包含的數字。When this method returns, contains the Version equivalent of the number that is contained in input, if the conversion succeeded. 如果 inputnull,則為 Empty;如果轉換失敗,當方法傳回時,result 則為 nullIf input is null, Empty, or if the conversion fails, result is null when the method returns.

傳回

Boolean

如果 true 參數轉換成功,則為 input,否則為 falsetrue if the input parameter was converted successfully; otherwise, false.

範例

下列範例 TryParse 會使用方法來剖析包含版本資訊的多個字串。The following example uses the TryParse method to parse a number of strings that contain version information.

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'.
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 方法類似,不同之處在于它不會在轉換失敗時擲回例外狀況。The TryParse method is similar to the Parse method, except that it doesn't throw an exception if the conversion fails. 相反地,它會傳回 false ,如果 input null、少於兩個或四個以上的元件、至少有一個元件不是整數、至少有一個元件小於零,或至少有一個元件大於 Int32.MaxValueInstead, it returns false if input is null, has fewer than two or more than four components, has at least one component that is not an integer, has at least one component that is less than zero, or has at least one component that is greater than Int32.MaxValue.

若要讓剖析作業成功, input 參數必須採用下列格式:For the parse operation to succeed, the input parameter must be in the following format:

major.minor[.build[.revision]]

其中 majorminorbuildrevision 是版本號碼的四個元件的字串表示:主要版本號碼、次要版本號碼、組建編號和修訂編號。where major, minor, build, and revision are the string representations of the version number's four components: major version number, minor version number, build number, and revision number, respectively. 選擇性元件會以方括弧括住 ( [和] ) 。Optional components are shown in square brackets ([ and ]). 元件必須依序顯示,且必須以句號分隔。The components must appear in order and must be separated by periods.

另請參閱

適用於

產品 Introduced