Version.Parse Method

Definition

Overloads

Parse(ReadOnlySpan<Char>)

Converts the specified read-only span of characters that represents a version number to an equivalent Version object.

Parse(String)

Converts the string representation of a version number to an equivalent Version object.

Parse(ReadOnlySpan<Char>)

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

Converts the specified read-only span of characters that represents a version number to an equivalent Version object.

C#
public static Version Parse(ReadOnlySpan<char> input);

Parameters

input
ReadOnlySpan<Char>

A read-only span of characters that contains a version number to convert.

Returns

An object that is equivalent to the version number specified in the input parameter.

Exceptions

input has fewer than two or more than four version components.

At least one component in input is less than zero.

At least one component in input is not an integer.

At least one component in input represents a number that is greater than Int32.MaxValue.

Remarks

The input parameter must have the following format:

major.minor[.build[.revision]]

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 the specified order and must be separated by periods.

Applies to

.NET 9 and other versions
Product Versions
.NET Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Standard 2.1

Parse(String)

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

Converts the string representation of a version number to an equivalent Version object.

C#
public static Version Parse(string input);

Parameters

input
String

A string that contains a version number to convert.

Returns

An object that is equivalent to the version number specified in the input parameter.

Exceptions

input is null.

input has fewer than two or more than four version components.

At least one component in input is less than zero.

At least one component in input is not an integer.

At least one component in input represents a number that is greater than Int32.MaxValue.

Examples

The following example uses the Parse method to parse a number of strings that contain version information.

C#
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'.

Remarks

The input parameter must have the following format:

major.minor[.build[.revision]]

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 the specified order and must be separated by periods.

Important

Because the string representation of a version number must conform to a recognized pattern, applications should always use exception handling when calling the Parse method to parse user input. Alternatively, you can call the TryParse method to parse the string representation of a version number and return a value that indicates whether the parse operation succeeded.

The Parse method is a convenience method; it is equivalent to calling the Version(String) constructor.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0