Version.TryParse Method

Definition

Overloads

TryParse(String, 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)

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)

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

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.

C#
public static bool TryParse(string input, out Version result);
C#
public static bool TryParse(string? input, out Version? result);

Parameters

input
String

A string that contains a version number to convert.

result
Version

When this method returns, contains the Version equivalent of the number that is contained in input, if the conversion succeeded. If input is null, Empty, or if the conversion fails, result is null when the method returns.

Returns

true if the input parameter was converted successfully; otherwise, false.

Examples

The following example uses the TryParse 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)
   {
      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'.

Remarks

The TryParse method is similar to the Parse method, except that it doesn't throw an exception if the conversion fails. Instead, 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.

For the parse operation to succeed, the input parameter must be in 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 order and must be separated by periods.

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

TryParse(ReadOnlySpan<Char>, Version)

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

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.

C#
public static bool TryParse(ReadOnlySpan<char> input, out Version? result);
C#
public static bool TryParse(ReadOnlySpan<char> input, out Version result);

Parameters

input
ReadOnlySpan<Char>

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

result
Version

When this method returns, contains the Version equivalent of the number that is contained in input, if the conversion succeeded. If input is null, Empty, or if the conversion fails, result is null when the method returns.

Returns

true if the input parameter was converted successfully; otherwise, false.

Remarks

The TryParse method is similar to the Parse method, except that it doesn't throw an exception if the conversion fails. Instead, 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.

For the parse operation to succeed, the input parameter must be in 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 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