System.Version Class

public sealed class Version : ICloneable, IComparable

Base Types

Object
  Version

This type implements ICloneable and IComparable.

Assembly

mscorlib

Library

BCL

Summary

Represents the version number of an assembly.

Description

Version numbers for an assembly consist of two to four components: major, minor, build, and revision. Components major and minor must be defined. Build and revision components are optional. Component revision may be used if and only if build is defined. All defined components must be a Int32 greater than or equal to zero.

[Note: By convention, the components are used as follows:

]

See Also

System Namespace

Members

Version Constructors

Version(int, int, int, int) Constructor
Version(int, int, int) Constructor
Version(int, int) Constructor
Version(System.String) Constructor
Version() Constructor

Version Methods

Version.Clone Method
Version.CompareTo Method
Version.Equals Method
Version.GetHashCode Method
Version.op_Equality Method
Version.op_GreaterThan Method
Version.op_GreaterThanOrEqual Method
Version.op_Inequality Method
Version.op_LessThan Method
Version.op_LessThanOrEqual Method

Version Properties

Version.Build Property
Version.Major Property
Version.Minor Property
Version.Revision Property


Version(int, int, int, int) Constructor

public Version(int major, int minor, int build, int revision);

Summary

Constructs and initializes a new instance of the Version class with the specified major, minor, build, and revision numbers.

Parameters

major
A Int32 specifying the major component.
minor
A Int32 specifying the minor component.
build
A Int32 specifying the build component.
revision
A Int32 specifying the revision component.

Exceptions

Exception TypeCondition
ArgumentOutOfRangeExceptionmajor, minor, build, or revision is less than zero.

Example

The following example sets the version to "6.1.2.4" and writes the result to the console.

using System;

public class Vers {
  public static void Main() {

    Version vers = new Version( 6, 1, 2, 4 );
    Console.WriteLine( "Version is {0}", vers.ToString() );
  }
}
   
The output is

Version is 6.1.2.4

See Also

System.Version Class, System Namespace

Version(int, int, int) Constructor

public Version(int major, int minor, int build);

Summary

Constructs and initializes a new instance of the Version class using the specified major, minor, and build values.

Parameters

major
A Int32 specifying the major component.
minor
A Int32 specifying the minor component.
build
A Int32 specifying the build component.

Exceptions

Exception TypeCondition
ArgumentOutOfRangeExceptionmajor, minor, or build is less than zero.

Example

The following example sets the version to "6.1.2" and writes the result to the console.

using System;

public class Vers {
  public static void Main() {

    Version vers = new Version( 6, 1, 2 );
    Console.WriteLine( "Version is {0}", vers.ToString() );
  }
}
   
The output is

Version is 6.1.2

See Also

System.Version Class, System Namespace

Version(int, int) Constructor

public Version(int major, int minor);

Summary

Constructs and initializes a new instance of the Version class using the specified major and minor values.

Parameters

major
A Int32 specifying the major component.
minor
A Int32 specifying the minor component.

Exceptions

Exception TypeCondition
ArgumentOutOfRangeExceptionmajor or minor is less than zero.

Example

The following example sets the version to "6.1" and writes the result to the console.

using System;

public class Vers {
  public static void Main() {

    Version vers = new Version( 6, 1 );
    Console.WriteLine( "Version is {0}", vers.ToString() );
  }
}
   
The output is

Version is 6.1

See Also

System.Version Class, System Namespace

Version(System.String) Constructor

public Version(string version);

Summary

Constructs and initializes a new instance of the Version class using the values represented by the specified String.

Parameters

version
A String that represents 2 to 4 Int32 integers separated by period characters ('.'). Each component delineated by a period character will be parsed to a Int32 with System.Int32.Parse(System.String)(String). The numbers will be processed in the following order: major, minor, build, revision. If the revision or the revision and the build components are not represented by version, their values will be undefined.

[Note: The formatting of version must be as follows, with optional components shown in square brackets ('[' and']'): major.minor[.build[.revision]], where each component returns a Int32 with System.Int32.Parse(System.String) (String).

]

Exceptions

Exception TypeCondition
ArgumentExceptionversion has fewer than 2 components or more than 4 components (i.e. fewer than 1 or more than 3 period characters).
ArgumentNullExceptionversion is a null reference.
ArgumentOutOfRangeExceptionmajor, minor, build, or revision is less than zero.
FormatExceptionAt least one component of version does not parse to a Int32 with System.Int32.Parse(System.String) (String).

Example

The following example sets the version to "6.1.2.4" and writes the result to the console.

using System;

public class Vers {
  public static void Main() {

    Version vers = new Version( "6.1.2.4" );
    Console.WriteLine( "Version is {0}", vers.ToString() );
  }
}
   
The output is

Version is 6.1.2.4

See Also

System.Version Class, System Namespace

Version() Constructor

public Version();

Summary

Constructs and initializes a new instance of the Version class.

Description

System.Version.Major and System.Version.Minor are set to zero. System.Version.Build and System.Version.Revision are unspecified.

See Also

System.Version Class, System Namespace

Version.Clone Method

public object Clone();

Summary

Returns a new Object with values equal to the property values of the current instance.

Return Value

A new Object whose values are equal to the property values of the current instance.

Description

The Object returned by this method must be explicitly cast to a Version before it may be used as one.

[Note: This method is implemented to support the ICloneable interface.]

Example

The following example clones the version number and writes the result to the console.

using System;
class VersionCloneExample {
  public static void Main() {
    Version vers = new Version("6.1.2.4");
    Console.WriteLine("The string representation of the" +
                      " version is {0}.",
                      vers.ToString());
    Version clone = (Version) vers.Clone();
    Console.WriteLine("The original version was" +
                      " successfully cloned.");
    Console.Write("The string representation of the" +
                  " cloned version is {0}.",
                  clone.ToString());
  }
}
   
The output is

The string representation of the version is 6.1.2.4.

The original version was successfully cloned.

The string representation of the cloned version is 6.1.2.4.

See Also

System.Version Class, System Namespace

Version.CompareTo Method

public int CompareTo(object version);

Summary

Returns the sort order of the current instance compared to the specified Object.

Parameters

version
The Object to compare to the current instance.

Return Value

A Int32 containing a value that reflects the sort order of the current instance as compared to version. The following table defines the conditions under which the returned value is a negative number, zero, or a positive number.

Return ValueDescription
Any negative number Current instance < version.
ZeroCurrent instance == version.
Any positive number Current instance > version, or version is a null reference.

Exceptions

Exception TypeCondition
ArgumentExceptionversion is not a Version and is not a null reference

Description

[Note: The components of Version in decreasing order of importance are: major, minor, build, and revision. An undefined component is assumed to be older than any defined component.

This method is implemented to support the IComparable interface.

]

Example

using System;
class VersionTest {
   static string Test ( Version v1, Version v2 ) {
      int i = v1.CompareTo(v2);
      if ( i < 0 )
         return "older than";
      else if ( i == 0 )
         return "the same as";
      else
         return "newer than";
   }
   public static void Main() {
      Version vers1 = new Version( "6.1.2.4" );
      Version vers2 = new Version( 6, 1 );
      Version vers3 = new Version( 6, 1, 3 );
      Console.Write("Version {0} is {1} ",
                    vers1, Test(vers1, vers2));
      Console.WriteLine("version {0}", vers2); 
      Console.Write("Version {0} is {1} ",
                    vers1, Test(vers1, vers3));
      Console.WriteLine("version {0}", vers3); 
      Console.Write("Version {0} is {1} ",
                    vers3, Test(vers3, vers3));
      Console.WriteLine("version {0}", vers3); 
      Console.Write("Version {0} is {1} ",
                    vers2, Test(vers2, vers1));
      Console.WriteLine("version {0}", vers1);
   }
}
   
The output is

Version 6.1.2.4 is newer than version 6.1

Version 6.1.2.4 is older than version 6.1.3

Version 6.1.3 is the same as version 6.1.3

Version 6.1 is older than version 6.1.2.4

See Also

System.Version Class, System Namespace

Version.Equals Method

public override bool Equals(object obj);

Summary

Determines whether the current instance and the specified Object represent the same type and value.

Parameters

obj
The Object to compare to the current instance.

Return Value

A Boolean where true indicates obj is the same type as the current instance and has equal System.Version.Major, System.Version.Minor, System.Version.Build, and System.Version.Revision properties as the current instance. If obj is a null reference or is not an instance of Version, returns false .

Description

[Note: This method overrides System.Object.Equals(System.Object).]

Example

using System;
class VersionEqualsExample {
   static void testEquals(Version v1, Version v2) {
      Console.Write("It is {0} that version ",
                    v1.Equals(v2));
      Console.WriteLine("{0} is equal to {1}.",
                    v1, v2);
   }
   public static void Main() {
      Version vers1 = new Version( "6.1.2.4" );
      Version vers2 = new Version( 6, 1 );
      testEquals( vers1, vers1 );
      testEquals( vers1, vers2 );
   }
}
   
The output is

It is True that version 6.1.2.4 is equal to 6.1.2.4.

It is False that version 6.1.2.4 is equal to 6.1.

See Also

System.Version Class, System Namespace

Version.GetHashCode Method

public override int GetHashCode();

Summary

Generates a hash code for the current instance.

Return Value

A Int32 containing the hash code for the current instance.

Description

The algorithm used to generate the hash code is unspecified.

[Note: This method overrides System.Object.GetHashCode.]

See Also

System.Version Class, System Namespace

Version.op_Equality Method

public static bool operator ==(Version v1, Version v2);

Summary

Determines whether two instances of Version are equal.

Parameters

v1
An instance of the Version class.
v2
An instance of the Version class.

Return Value

A Boolean where true indicates v1 and v2 have equal System.Version.Major, System.Version.Minor, System.Version.Build, and System.Version.Revision properties, or both v1 and v2 are null ; otherwise false .

Description

The parts of the version number are compared independently starting with the System.Version.Major property and then the System.Version.Minor, System.Version.Build, and System.Version.Revision properties, in order. This method returns as soon as one of the properties is determined not to be equal.

See Also

System.Version Class, System Namespace

Version.op_GreaterThan Method

public static bool operator >(Version v1, Version v2);

Summary

Determines whether the first instance of Version is greater than the second instance of Version.

Parameters

v1
An instance of the Version class.
v2
An instance of the Version class.

Return Value

A Boolean where true indicates v1 is greater than v2; otherwise false . If v1 is null , false is returned.

Exceptions

Exception TypeCondition
ArgumentNullExceptionv2 is a null reference.

Description

The parts of the version number are compared independently starting with the System.Version.Major property and then the System.Version.Minor, System.Version.Build, and System.Version.Revision properties, in order. This method returns as soon as one of the properties is determined not to be equal.

See Also

System.Version Class, System Namespace

Version.op_GreaterThanOrEqual Method

public static bool operator >=(Version v1, Version v2);

Summary

Determines whether the first instance of Version is greater than or equal to the second instance of Version.

Parameters

v1
An instance of the Version class.
v2
An instance of the Version class.

Return Value

A Boolean where true indicates v1 is greater than or equal to v2; otherwise false . If v1 is null , false is returned.

Exceptions

Exception TypeCondition
ArgumentNullExceptionv2 is a null reference.

Description

The parts of the version number are compared independently starting with the System.Version.Major property and then the System.Version.Minor, System.Version.Build, and System.Version.Revision properties, in order. This method returns as soon as one of the properties is determined not to be equal.

See Also

System.Version Class, System Namespace

Version.op_Inequality Method

public static bool operator !=(Version v1, Version v2);

Summary

Determines whether two instances of Version are not equal.

Parameters

v1
An instance of the Version class.
v2
An instance of the Version class.

Return Value

A Boolean where true indicates v1 and v2 have at least one unequal property; otherwise false . If v1 and v2 are both null , returns false; if one is null but not the other, returns true .

Description

The parts of the version number are compared independently starting with the System.Version.Major property and then the System.Version.Minor, System.Version.Build, and System.Version.Revision properties, in order. This method returns as soon as one of the properties is determined not to be equal.

See Also

System.Version Class, System Namespace

Version.op_LessThan Method

public static bool operator  <(Version v1, Version v2);

Summary

Determines whether the first instance of Version is less than the second instance of Version.

Parameters

v1
An instance of the Version class.
v2
An instance of the Version class.

Return Value

A Boolean where true indicates v1 is less than v2; otherwise false . If v2 is null , false is returned.

Exceptions

Exception TypeCondition
ArgumentNullExceptionv1 is a null reference.

Description

The parts of the version number are compared independently starting with the System.Version.Major property and then the System.Version.Minor, System.Version.Build, and System.Version.Revision properties, in order. This method returns as soon as one of the properties is determined not to be equal.

See Also

System.Version Class, System Namespace

Version.op_LessThanOrEqual Method

public static bool operator <=(Version v1, Version v2);

Summary

Determines whether the first instance of Version is less than or equal to the second instance of Version.

Parameters

v1
An instance of the Version class.
v2
An instance of the Version class.

Return Value

A Boolean where true indicates v1 is less than or equal to v2; otherwise false . If v2 is null , false is returned.

Exceptions

Exception TypeCondition
ArgumentNullExceptionv1 is a null reference.

Description

The parts of the version number are compared independently starting with the System.Version.Major property and then the System.Version.Minor, System.Version.Build, and System.Version.Revision properties, in order. This method returns as soon as one of the properties is determined not to be equal.

See Also

System.Version Class, System Namespace

Version.Build Property

public int Build { get; }

Summary

Gets the value of the build component of the current instance.

Property Value

A Int32 specifying the build component, or -1 if the build component is undefined.

Description

This property is read-only.

[Note: If the version number is 6.1.2.4, the build component is 2. If the version number is 6.1, the build component is -1, which is considered to be undefined.]

Example

using System;
class VersionBuildExample {
   public static void Main() {
      Version vers = new Version("6.1.2.4");
      Console.Write("The build component of ");
      Console.WriteLine("version vers = {0}.", vers.Build);
   }
}
   
The output is

The build component of version vers = 2.

See Also

System.Version Class, System Namespace

Version.Major Property

public int Major { get; }

Summary

Gets the value of the major component of the current instance.

Property Value

A Int32 specifying the major component.

Description

This property is read-only.

[Example: If the version number is 6.1, the major version is 6.]

Example

using System;
class VersionMajorExample {
   public static void Main() {
      Version vers = new Version("6.1.2.4");
      Console.Write("The major component ");
      Console.WriteLine("of version vers = {0}.",
                        vers.Major);
   }
}
   
The output is

The major component of version vers = 6.

See Also

System.Version Class, System Namespace

Version.Minor Property

public int Minor { get; }

Summary

Gets the value of the minor component of the current instance.

Property Value

A Int32 specifying the minor component.

Description

This property is read-only.

[Example: If the version number is 6.1, the minor component is 1.]

Example

using System;
class VersionMinorExample {
   public static void Main() {
      Version vers = new Version("6.1.2.4");
      Console.Write("The minor component ");
      Console.WriteLine("of version vers = {0}.",
                        vers.Minor);
   }
}
   
The output is

The minor component of version vers = 1.

See Also

System.Version Class, System Namespace

Version.Revision Property

public int Revision { get; }

Summary

Gets the value of the revision component of the current instance.

Property Value

A Int32 specifying the revision component, or -1 if the revision component is undefined.

Description

This property is read-only.

[Example: If the version number is 6.1.2.4, the revision component is 4. If the version number is 6.1, the revision component is considered to be undefined.]

Example

using System;
class VersionRevisionExample {
   public static void Main() {
      Version vers = new Version("6.1.2.4");
      Console.Write("The revision component of ");
      Console.WriteLine("version vers = {0}.",
                        vers.Revision);
   }
}
   
The output is

The revision component of version vers = 4.

See Also

System.Version Class, System Namespace