Version.Parse メソッド

定義

オーバーロード

Parse(ReadOnlySpan<Char>)

バージョン番号を表す文字の指定した読み取り専用のスパンを、等価の Version オブジェクトに変換します。

Parse(String)

バージョン番号の文字列形式を等価の Version オブジェクトに変換します。

Parse(ReadOnlySpan<Char>)

Source:
Version.cs
Source:
Version.cs
Source:
Version.cs

バージョン番号を表す文字の指定した読み取り専用のスパンを、等価の Version オブジェクトに変換します。

public:
 static Version ^ Parse(ReadOnlySpan<char> input);
public static Version Parse (ReadOnlySpan<char> input);
static member Parse : ReadOnlySpan<char> -> Version
Public Shared Function Parse (input As ReadOnlySpan(Of Char)) As Version

パラメーター

input
ReadOnlySpan<Char>

変換するバージョン番号を含む文字の読み取り専用のスパン。

戻り値

input パラメーターで指定されているバージョン番号と等価のオブジェクト。

例外

input のバージョン構成要素数が 2 未満であるか、4 を上回っています。

input 内の少なくとも 1 つの構成要素が 0 未満です。

input 内の少なくとも 1 つの構成要素が整数ではありません。

内の input 少なくとも 1 つのコンポーネントは、 Int32.MaxValue より大きい数値を表します。

注釈

パラメーターの形式は input 次のとおりです。

major.minor[.build[.revision]]

ここで majorminor、、 buildおよび revision は、バージョン番号の 4 つのコンポーネント (メジャー バージョン番号、マイナー バージョン番号、ビルド番号、リビジョン番号) の文字列表現です。 オプションのコンポーネントは角かっこ ([ と ]) で示されます。 コンポーネントは、指定した順序で表示する必要があり、期間で区切る必要があります。

適用対象

Parse(String)

Source:
Version.cs
Source:
Version.cs
Source:
Version.cs

バージョン番号の文字列形式を等価の Version オブジェクトに変換します。

public:
 static Version ^ Parse(System::String ^ input);
public static Version Parse (string input);
static member Parse : string -> Version
Public Shared Function Parse (input As String) As Version

パラメーター

input
String

変換するバージョン番号を含んだ文字列。

戻り値

input パラメーターで指定されているバージョン番号と等価のオブジェクト。

例外

inputnullです。

input のバージョン構成要素数が 2 未満であるか、4 を上回っています。

input 内の少なくとも 1 つの構成要素が 0 未満です。

input 内の少なくとも 1 つの構成要素が整数ではありません。

内の input 少なくとも 1 つのコンポーネントは、 Int32.MaxValue より大きい数値を表します。

次の例では、 メソッドを Parse 使用して、バージョン情報を含む複数の文字列を解析します。

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)
   {
      try {
         Version ver = Version.Parse(input);
         Console.WriteLine("Converted '{0} to {1}.", input, ver);
      }
      catch (ArgumentNullException) {
         Console.WriteLine("Error: String to be parsed is null.");
      }
      catch (ArgumentOutOfRangeException) {
         Console.WriteLine("Error: Negative value in '{0}'.", input);
      }
      catch (ArgumentException) {
         Console.WriteLine("Error: Bad number of components in '{0}'.", 
                           input);
      }
      catch (FormatException) {
         Console.WriteLine("Error: Non-integer value in '{0}'.", input);
      }
      catch (OverflowException) {   
         Console.WriteLine("Error: Number out of range in '{0}'.", input);                  
      }   
   }
}
// The example displays the following output:
//       Converted '4.0 to 4.0.
//       Error: Non-integer value in '4.0.'.
//       Converted '1.1.2 to 1.1.2.
//       Converted '1.1.2.01702 to 1.1.2.1702.
//       Error: Bad number of components in '1.1.2.0702.119'.
//       Error: Number out of range in '1.3.5.2150000000'.
open System

let parseVersion (input: string) =
    try
        let ver = Version.Parse input
        printfn $"Converted '{input} to {ver}."
    with
    | :? ArgumentNullException ->
        printfn "Error: String to be parsed is null."
    | :? ArgumentOutOfRangeException ->
        printfn $"Error: Negative value in '{input}'."
    | :? ArgumentException ->
        printfn $"Error: Bad number of components in '{input}'."
    | :? FormatException ->
        printfn $"Error: Non-integer value in '{input}'."
    | :? OverflowException ->
        printfn $"Error: Number out of range in '{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.
//       Error: Non-integer value in '4.0.'.
//       Converted '1.1.2 to 1.1.2.
//       Converted '1.1.2.01702 to 1.1.2.1702.
//       Error: Bad number of components in '1.1.2.0702.119'.
//       Error: Number out of range in '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)
      Try
         Dim ver As Version = Version.Parse(input)
         Console.WriteLine("Converted '{0} to {1}.", input, ver)
      Catch e As ArgumentNullException
         Console.WriteLine("Error: String to be parsed is null.")
      Catch e As ArgumentOutOfRangeException
         Console.WriteLine("Error: Negative value in '{0}'.", input)
      Catch e As ArgumentException
         Console.WriteLine("Error: Bad number of components in '{0}'.", 
                           input)
      Catch e As FormatException
         Console.WriteLine("Error: Non-integer value in '{0}'.", input)
      Catch e As OverflowException   
         Console.WriteLine("Error: Number out of range in '{0}'.", input)                  
      End Try   
   End Sub
End Module
' The example displays the following output:
'       Converted '4.0 to 4.0.
'       Error: Non-integer value in '4.0.'.
'       Converted '1.1.2 to 1.1.2.
'       Converted '1.1.2.01702 to 1.1.2.1702.
'       Error: Bad number of components in '1.1.2.0702.119'.
'       Error: Number out of range in '1.3.5.2150000000'.

注釈

パラメーターの形式は input 次のとおりです。

major.minor[.build[.revision]]

ここで majorminor、、 buildおよび revision は、バージョン番号の 4 つのコンポーネント (メジャー バージョン番号、マイナー バージョン番号、ビルド番号、リビジョン番号) の文字列表現です。 オプションのコンポーネントは角かっこ ([ と ]) で示されます。 コンポーネントは、指定した順序で表示する必要があり、期間で区切る必要があります。

重要

バージョン番号の文字列表現は認識されたパターンに準拠している必要があるため、アプリケーションはメソッドを呼び出してユーザー入力を解析するときに常に例外処理を Parse 使用する必要があります。 または、 メソッドを TryParse 呼び出してバージョン番号の文字列表現を解析し、解析操作が成功したかどうかを示す値を返すこともできます。

メソッドは Parse 便利なメソッドであり、コンストラクターを呼び出すことと Version(String) 同じです。

こちらもご覧ください

適用対象