Version.TryParse 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
重载
| 名称 | 说明 |
|---|---|
| TryParse(ReadOnlySpan<Byte>, Version) |
尝试将版本号的 UTF-8 表示形式转换为等效的 Version 对象,并返回一个值,该值指示转换是否成功。 |
| TryParse(String, Version) |
尝试将版本号的字符串表示形式转换为等效 Version 对象,并返回一个值,该值指示转换是否成功。 |
| TryParse(ReadOnlySpan<Char>, Version) |
尝试将表示版本号的指定只读字符范围转换为等效 Version 对象,并返回一个值,该值指示转换是否成功。 |
TryParse(ReadOnlySpan<Byte>, Version)
- Source:
- Version.cs
- Source:
- Version.cs
尝试将版本号的 UTF-8 表示形式转换为等效的 Version 对象,并返回一个值,该值指示转换是否成功。
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 -> bool
Public Shared Function TryParse (utf8Text As ReadOnlySpan(Of Byte), ByRef result As Version) As Boolean
参数
- utf8Text
- ReadOnlySpan<Byte>
要分析的 UTF-8 字符的范围。
返回
适用于
TryParse(String, Version)
- Source:
- Version.cs
- 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 -> bool
Public Shared Function TryParse (input As String, ByRef result As Version) As Boolean
参数
- input
- String
包含要转换的版本号的字符串。
- result
- Version
此方法返回时,如果转换成功,则 Version 包含所 input包含数字的等效项。 如果是input,null或者转换失败,Empty则为result方法返回时。null
返回
示例
以下示例使用 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 该方法类似,只是转换失败时不会引发异常。 相反,它返回false的是inputnull,如果小于两个或四个组件,至少有一个组件不是整数,至少有一个小于零的组件,或者至少有一个大于Int32.MaxValue的组件。
若要使分析操作成功,参数 input 必须采用以下格式:
major.minor[.build[.revision]]
其中major,minor以及buildrevision版本号的四个组件的字符串表示形式:主要版本号、次要版本号、内部版本号和修订号。 可选组件以方括号 ([ 和 ]) 显示。 组件必须按顺序显示,并且必须按句点分隔。
另请参阅
适用于
TryParse(ReadOnlySpan<Char>, Version)
- Source:
- Version.cs
- 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 -> bool
Public Shared Function TryParse (input As ReadOnlySpan(Of Char), ByRef result As Version) As Boolean
参数
- input
- ReadOnlySpan<Char>
包含要转换的版本号的字符的只读范围。
- result
- Version
此方法返回时,如果转换成功,则 Version 包含所 input包含数字的等效项。 如果是input,null或者转换失败,Empty则为result方法返回时。null
返回
注解
该方法 TryParse 与 Parse 该方法类似,只是转换失败时不会引发异常。 相反,它返回false的是inputnull,如果小于两个或四个组件,至少有一个组件不是整数,至少有一个小于零的组件,或者至少有一个大于Int32.MaxValue的组件。
若要使分析操作成功,参数 input 必须采用以下格式:
major.minor[.build[.revision]]
其中major,minor以及buildrevision版本号的四个组件的字符串表示形式:主要版本号、次要版本号、内部版本号和修订号。 可选组件以方括号 ([ 和 ]) 显示。 组件必须按顺序显示,并且必须按句点分隔。