public sealed class Version : ICloneable, IComparable
Object
VersionThis type implements ICloneable and IComparable.
mscorlib
BCL
Represents the version number of an assembly.
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:
]
- Major: Assemblies with the same name but different major versions are not interchangeable. This would be appropriate, for example, for a major rewrite of a product where backwards compatibility cannot be assumed.
- Minor: If the name and major number on two assemblies are the same, but the minor number is different, this indicates significant enhancement with the intention of backwards compatibility. This would be appropriate, for example, on a "point release" of a product or a fully backward compatible new version of a product.
- Assemblies with the same name, major, and minor version numbers but different revisions are intended to be fully interchangeable. This would be appropriate, for example, to fix a security hole in a previously released assembly.
- A difference in build number is intended to represent a recompilation of the same source. This would be appropriate, for example, because of processor, platform, or compiler changes.
System Namespace
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
public Version(int major, int minor, int build, int revision);
Constructs and initializes a new instance of the Version class with the specified major, minor, build, and revision numbers.
- 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.
Exception Type Condition ArgumentOutOfRangeException major, minor, build, or revision is less than zero.
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 isVersion is 6.1.2.4
System.Version Class, System Namespace
public Version(int major, int minor, int build);
Constructs and initializes a new instance of the Version class using the specified major, minor, and build values.
- major
- A Int32 specifying the major component.
- minor
- A Int32 specifying the minor component.
- build
- A Int32 specifying the build component.
Exception Type Condition ArgumentOutOfRangeException major, minor, or build is less than zero.
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 isVersion is 6.1.2
System.Version Class, System Namespace
public Version(int major, int minor);
Constructs and initializes a new instance of the Version class using the specified major and minor values.
- major
- A Int32 specifying the major component.
- minor
- A Int32 specifying the minor component.
Exception Type Condition ArgumentOutOfRangeException major or minor is less than zero.
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 isVersion is 6.1
System.Version Class, System Namespace
public Version(string version);
Constructs and initializes a new instance of the Version class using the values represented by the specified String.
- 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).
]
Exception Type Condition ArgumentException version has fewer than 2 components or more than 4 components (i.e. fewer than 1 or more than 3 period characters). ArgumentNullException version is a null reference. ArgumentOutOfRangeException major, minor, build, or revision is less than zero. FormatException At least one component of version does not parse to a Int32 with System.Int32.Parse(System.String) (String).
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 isVersion is 6.1.2.4
System.Version Class, System Namespace
public Version();
Constructs and initializes a new instance of the Version class.
System.Version.Major and System.Version.Minor are set to zero. System.Version.Build and System.Version.Revision are unspecified.
System.Version Class, System Namespace
public object Clone();
Returns a new Object with values equal to the property values of the current instance.
A new Object whose values are equal to the property values of the current instance.
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.]
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.
System.Version Class, System Namespace
public int CompareTo(object version);
Returns the sort order of the current instance compared to the specified Object.
- version
- The Object to compare to the current instance.
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 Value Description Any negative number Current instance < version. Zero Current instance == version. Any positive number Current instance > version, or version is a null reference.
Exception Type Condition ArgumentException version is not a Version and is not a null reference
[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.
]
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
System.Version Class, System Namespace
public override bool Equals(object obj);
Determines whether the current instance and the specified Object represent the same type and value.
- obj
- The Object to compare to the current instance.
A Boolean wheretrue
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, returnsfalse
.
[Note: This method overrides System.Object.Equals(System.Object).]
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.
System.Version Class, System Namespace
public override int GetHashCode();
Generates a hash code for the current instance.
A Int32 containing the hash code for the current instance.
The algorithm used to generate the hash code is unspecified.[Note: This method overrides System.Object.GetHashCode.]
System.Version Class, System Namespace
public static bool operator ==(Version v1, Version v2);
Determines whether two instances of Version are equal.
- v1
- An instance of the Version class.
- v2
- An instance of the Version class.
A Boolean wheretrue
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 arenull
; otherwisefalse
.
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.
System.Version Class, System Namespace
public static bool operator >(Version v1, Version v2);
Determines whether the first instance of Version is greater than the second instance of Version.
- v1
- An instance of the Version class.
- v2
- An instance of the Version class.
A Boolean wheretrue
indicates v1 is greater than v2; otherwisefalse
. If v1 isnull
,false
is returned.
Exception Type Condition ArgumentNullException v2 is a null
reference.
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.
System.Version Class, System Namespace
public static bool operator >=(Version v1, Version v2);
Determines whether the first instance of Version is greater than or equal to the second instance of Version.
- v1
- An instance of the Version class.
- v2
- An instance of the Version class.
A Boolean wheretrue
indicates v1 is greater than or equal to v2; otherwisefalse
. If v1 isnull
,false
is returned.
Exception Type Condition ArgumentNullException v2 is a null
reference.
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.
System.Version Class, System Namespace
public static bool operator !=(Version v1, Version v2);
Determines whether two instances of Version are not equal.
- v1
- An instance of the Version class.
- v2
- An instance of the Version class.
A Boolean wheretrue
indicates v1 and v2 have at least one unequal property; otherwisefalse
. If v1 and v2 are bothnull
, returns false; if one isnull
but not the other, returnstrue
.
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.
System.Version Class, System Namespace
public static bool operator <(Version v1, Version v2);
Determines whether the first instance of Version is less than the second instance of Version.
- v1
- An instance of the Version class.
- v2
- An instance of the Version class.
A Boolean wheretrue
indicates v1 is less than v2; otherwisefalse
. If v2 isnull
,false
is returned.
Exception Type Condition ArgumentNullException v1 is a null
reference.
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.
System.Version Class, System Namespace
public static bool operator <=(Version v1, Version v2);
Determines whether the first instance of Version is less than or equal to the second instance of Version.
- v1
- An instance of the Version class.
- v2
- An instance of the Version class.
A Boolean wheretrue
indicates v1 is less than or equal to v2; otherwisefalse
. If v2 isnull
,false
is returned.
Exception Type Condition ArgumentNullException v1 is a null
reference.
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.
System.Version Class, System Namespace
public int Build { get; }
Gets the value of the build component of the current instance.
A Int32 specifying the build component, or -1 if the build component is undefined.
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.]
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.
System.Version Class, System Namespace
public int Major { get; }
Gets the value of the major component of the current instance.
A Int32 specifying the major component.
This property is read-only.[Example: If the version number is 6.1, the major version is 6.]
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.
System.Version Class, System Namespace
public int Minor { get; }
Gets the value of the minor component of the current instance.
A Int32 specifying the minor component.
This property is read-only.[Example: If the version number is 6.1, the minor component is 1.]
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.
System.Version Class, System Namespace
public int Revision { get; }
Gets the value of the revision component of the current instance.
A Int32 specifying the revision component, or -1 if the revision component is undefined.
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.]
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.
System.Version Class, System Namespace