C# Version TryParse

Description

Version TryParse 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.

Syntax

Version.TryParse has the following syntax.


public static bool TryParse(
  string input,
  out Version result
)

Parameters

Version.TryParse has the following parameters.

  • input - A string that contains a version number to convert.
  • result - When this method returns, contains the Version equivalent of the number that is contained in input, if the conversion succeeded, or a Version object whose major and minor version numbers are 0 if the conversion failed. If input is null or String.Empty, result is null when the method returns.

Returns

Version.TryParse method returns true if the input parameter was converted successfully; otherwise, false.

Example

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


// w  ww  .j ava 2s.  co m
using System;

public class Example
{
   public static void Main()
   {
      string input = "4.0";
      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 code above generates the following result.





















Home »
  C# Tutorial »
    System »




Array
BitConverter
Boolean
Byte
Char
Console
ConsoleKeyInfo
Convert
DateTime
DateTimeOffset
Decimal
Double
Enum
Environment
Exception
Guid
Int16
Int32
Int64
Math
OperatingSystem
Random
SByte
Single
String
StringComparer
TimeSpan
TimeZone
TimeZoneInfo
Tuple
Tuple
Tuple
Type
UInt16
UInt32
UInt64
Uri
Version