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);
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

当此方法返回时,如果转换成功,则为 Version 总所含数字的等效 inputWhen this method returns, the Version equivalent of the number that is contained in input, if the conversion succeeded. 如果 inputnullEmpty 或者如果转换失败,当此方法返回时,result 则为 nullIf input is null, Empty, or if the conversion fails, result is null when the method returns.

返回

如果 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 如果 inputnull,具有少于两个或四个以上的组件,至少有一个组件不是整数,至少有一个组件小于0,或者至少有一个组件大于 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.

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 中所包含的数字等效的 inputWhen this method returns, contains the Version equivalent of the number that is contained in input, if the conversion succeeded. 如果 inputnullEmpty 或者如果转换失败,当此方法返回时,result 则为 nullIf input is null, Empty, or if the conversion fails, result is null when the method returns.

返回

如果 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 如果 inputnull,具有少于两个或四个以上的组件,至少有一个组件不是整数,至少有一个组件小于0,或者至少有一个组件大于 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.

另请参阅

适用于