Version Class
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Represents the version number of an assembly, operating system, or the common language runtime. This class cannot be inherited.
public ref class Version sealed : IComparable, IComparable<Version ^>, IEquatable<Version ^>
public ref class Version sealed : ICloneable, IComparable, IComparable<Version ^>, IEquatable<Version ^>
public ref class Version sealed : ICloneable, IComparable, IComparable<Version ^>, IEquatable<Version ^>, ISpanFormattable
public ref class Version sealed : ICloneable, IComparable, IComparable<Version ^>, IEquatable<Version ^>, ISpanFormattable, IUtf8SpanFormattable
public ref class Version sealed : ICloneable, IComparable
public sealed class Version : IComparable, IComparable<Version>, IEquatable<Version>
public sealed class Version : ICloneable, IComparable, IComparable<Version>, IEquatable<Version>
public sealed class Version : ICloneable, IComparable, IComparable<Version>, IEquatable<Version>, ISpanFormattable
public sealed class Version : ICloneable, IComparable, IComparable<Version>, IEquatable<Version>, ISpanFormattable, IUtf8SpanFormattable
[System.Serializable]
public sealed class Version : ICloneable, IComparable
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public sealed class Version : ICloneable, IComparable, IComparable<Version>, IEquatable<Version>
type Version = class
interface IComparable
interface IComparable<Version>
interface IEquatable<Version>
type Version = class
interface ICloneable
interface IComparable
interface IComparable<Version>
interface IEquatable<Version>
type Version = class
interface ICloneable
interface IComparable
interface IComparable<Version>
interface IEquatable<Version>
interface ISpanFormattable
interface IFormattable
type Version = class
interface ICloneable
interface IComparable
interface IComparable<Version>
interface IEquatable<Version>
interface IFormattable
interface ISpanFormattable
type Version = class
interface ICloneable
interface IComparable
interface IComparable<Version>
interface IEquatable<Version>
interface IFormattable
interface ISpanFormattable
interface IUtf8SpanFormattable
type Version = class
interface IComparable
interface IComparable<Version>
interface IEquatable<Version>
interface ICloneable
[<System.Serializable>]
type Version = class
interface ICloneable
interface IComparable
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type Version = class
interface ICloneable
interface IComparable
interface IComparable<Version>
interface IEquatable<Version>
Public NotInheritable Class Version
Implements IComparable, IComparable(Of Version), IEquatable(Of Version)
Public NotInheritable Class Version
Implements ICloneable, IComparable, IComparable(Of Version), IEquatable(Of Version)
Public NotInheritable Class Version
Implements ICloneable, IComparable, IComparable(Of Version), IEquatable(Of Version), ISpanFormattable
Public NotInheritable Class Version
Implements ICloneable, IComparable, IComparable(Of Version), IEquatable(Of Version), ISpanFormattable, IUtf8SpanFormattable
Public NotInheritable Class Version
Implements ICloneable, IComparable
- Inheritance
-
Version
- Attributes
- Implements
Examples
The following example uses the AssemblyVersionAttribute attribute to assign a version number to an assembly. At compile time, this version information is stored with the assembly's metadata. At run time, the example retrieves the value of the Type.Assembly property on a type found in the assembly to get a reference to the executing assembly, and it retrieves the assembly's version information from the Version property of the AssemblyName object returned by the Assembly.GetName method.
using System;
using System.Reflection;
[assembly:AssemblyVersionAttribute("2.0.1")]
public class Example1
{
public static void Main()
{
Assembly thisAssem = typeof(Example1).Assembly;
AssemblyName thisAssemName = thisAssem.GetName();
Version ver = thisAssemName.Version;
Console.WriteLine("This is version {0} of {1}.", ver, thisAssemName.Name);
}
}
// The example displays the following output:
// This is version 2.0.1.0 of Example1.
open System.Reflection
[<assembly: AssemblyVersionAttribute "2.0.1">]
do ()
type Example1 = class end
let thisAssem = typeof<Example1>.Assembly
let thisAssemName = thisAssem.GetName()
let ver = thisAssemName.Version
printfn $"This is version {ver} of {thisAssemName.Name}."
// The example displays the following output:
// This is version 2.0.1.0 of Example1.
Imports System.Reflection
<Assembly:AssemblyVersionAttribute("2.0.1")>
Module Example1
Public Sub Main()
Dim thisAssem As Assembly = GetType(Example1).Assembly
Dim thisAssemName As AssemblyName = thisAssem.GetName()
Dim ver As Version = thisAssemName.Version
Console.WriteLine("This is version {0} of {1}.", ver, thisAssemName.Name)
End Sub
End Module
' The example displays the following output:
' This is version 2.0.1.0 of Example1.
Remarks
Version numbers consist of two to four components: major, minor, build, and revision. The major and minor components are required; the build and revision components are optional, but the build component is required if the revision component is defined. All defined components must be integers greater than or equal to 0. The format of the version number is as follows (optional components are shown in square brackets ([ and ]):
major.minor[.build[.revision]]
The components are used by convention as follows:
Major: Assemblies with the same name but different major versions are not interchangeable. A higher version number might indicate a major rewrite of a product where backward compatibility cannot be assumed.
Minor: If the name and major version number on two assemblies are the same, but the minor version number is different, this indicates significant enhancement with the intention of backward compatibility. This higher minor version number might indicate a point release of a product or a fully backward-compatible new version of a product.
Build: A difference in build number represents a recompilation of the same source. Different build numbers might be used when the processor, platform, or compiler changes.
Revision: Assemblies with the same name, major, and minor version numbers but different revisions are intended to be fully interchangeable. A higher revision number might be used in a build that fixes a security hole in a previously released assembly.
Subsequent versions of an assembly that differ only by build or revision numbers are considered to be Hotfix updates of the prior version.
Important
The value of Version properties that have not been explicitly assigned a value is undefined (-1).
Starting with .NET Framework 2.0, the MajorRevision and MinorRevision properties enable you to identify a temporary version of your application that, for example, corrects a problem until you can release a permanent solution. Furthermore, the Windows NT operating system uses the MajorRevision property to encode the service pack number.
Assigning Version Information to Assemblies
Ordinarily, the Version class is not used to assign a version number to an assembly. Instead, the AssemblyVersionAttribute class is used to define an assembly's version, as illustrated by the example in this topic.
Retrieving Version Information
Version objects are most frequently used to store version information about some system or application component (such as the operating system), the common language runtime, the current application's executable, or a particular assembly. The following examples illustrate some of the most common scenarios:
Retrieving the operating system version. The following example uses the OperatingSystem.Version property to retrieve the version number of the operating system.
// Get the operating system version. OperatingSystem os = Environment.OSVersion; Version ver = os.Version; Console.WriteLine("Operating System: {0} ({1})", os.VersionString, ver.ToString());
// Get the operating system version. let os = Environment.OSVersion let ver = os.Version printfn $"Operating System: {os.VersionString} ({ver})"
' Get the operating system version. Dim os As OperatingSystem = Environment.OSVersion Dim ver As Version = os.Version Console.WriteLine("Operating System: {0} ({1})", os.VersionString, ver.ToString())
Retrieving the version of the common language runtime. The following example uses the Environment.Version property to retrieve version information about the common language runtime.
// Get the common language runtime version. Version ver = Environment.Version; Console.WriteLine("CLR Version {0}", ver.ToString());
// Get the common language runtime version. let ver = Environment.Version printfn $"CLR Version {ver}"
' Get the common language runtime version. Dim ver As Version = Environment.Version Console.WriteLine("CLR Version {0}", ver.ToString())
Retrieving the current application's assembly version. The following example uses the Assembly.GetEntryAssembly method to obtain a reference to an Assembly object that represents the application executable and then retrieves its assembly version number.
using System; using System.Reflection; public class Example { public static void Main() { // Get the version of the executing assembly (that is, this assembly). Assembly assem = Assembly.GetEntryAssembly(); AssemblyName assemName = assem.GetName(); Version ver = assemName.Version; Console.WriteLine("Application {0}, Version {1}", assemName.Name, ver.ToString()); } }
open System.Reflection // Get the version of the executing assembly (that is, this assembly). let assem = Assembly.GetEntryAssembly() let assemName = assem.GetName() let ver = assemName.Version printfn $"Application {assemName.Name}, Version {ver}"
Imports System.Reflection Module Example Public Sub Main() ' Get the version of the executing assembly (that is, this assembly). Dim assem As Assembly = Assembly.GetEntryAssembly() Dim assemName As AssemblyName = assem.GetName() Dim ver As Version = assemName.Version Console.WriteLine("Application {0}, Version {1}", assemName.Name, ver.ToString()) End Sub End Module
Retrieving the current assembly's assembly version. The following example uses the Type.Assembly property to obtain a reference to an Assembly object that represents the assembly that contains the application entry point, and then retrieves its version information.
using System; using System.Reflection; public class Example { public static void Main() { // Get the version of the current assembly. Assembly assem = typeof(Example).Assembly; AssemblyName assemName = assem.GetName(); Version ver = assemName.Version; Console.WriteLine("{0}, Version {1}", assemName.Name, ver.ToString()); } }
type Example = class end // Get the version of the current assembly. let assem = typeof<Example>.Assembly let assemName = assem.GetName() let ver = assemName.Version printfn $"{assemName.Name}, Version {ver}"
Imports System.Reflection Module Example Public Sub Main() ' Get the version of the current assembly. Dim assem As Assembly = GetType(Example).Assembly Dim assemName As AssemblyName = assem.GetName() Dim ver As Version = assemName.Version Console.WriteLine("{0}, Version {1}", assemName.Name, ver.ToString()) VB End Sub End Module
Retrieving the version of a specific assembly. The following example uses the Assembly.ReflectionOnlyLoadFrom method to obtain a reference to an Assembly object that has a particular file name, and then retrieves its version information. Note that several other methods also exist to instantiate an Assembly object by file name or by strong name.
using System; using System.Reflection; public class Example { public static void Main() { // Get the version of a specific assembly. string filename = @".\StringLibrary.dll"; Assembly assem = Assembly.ReflectionOnlyLoadFrom(filename); AssemblyName assemName = assem.GetName(); Version ver = assemName.Version; Console.WriteLine("{0}, Version {1}", assemName.Name, ver.ToString()); } }
open System.Reflection // Get the version of a specific assembly. let filename = @".\StringLibrary.dll" let assem = Assembly.ReflectionOnlyLoadFrom filename let assemName = assem.GetName() let ver = assemName.Version printfn $"{assemName.Name}, Version {ver}"
Imports System.Reflection Module Example Public Sub Main() ' Get the version of a specific assembly. Dim filename As String = ".\StringLibrary.dll" Dim assem As Assembly = Assembly.ReflectionOnlyLoadFrom(filename) Dim assemName As AssemblyName = assem.GetName() Dim ver As Version = assemName.Version Console.WriteLine("{0}, Version {1}", assemName.Name, ver.ToString()) End Sub End Module
Retrieving the Publish Version of a ClickOnce application. The following example uses the ApplicationDeployment.CurrentVersion property to display an application's Publish Version. Note that its successful execution requires the example's application identity to be set. This is handled automatically by the Visual Studio Publish Wizard.
using System; using System.Deployment.Application; public class Example { public static void Main() { Version ver = ApplicationDeployment.CurrentDeployment.CurrentVersion; Console.WriteLine("ClickOnce Publish Version: {0}", ver); } }
Imports System.Deployment.Application Module Example Public Sub Main() Dim ver As Version = ApplicationDeployment.CurrentDeployment.CurrentVersion Console.WriteLine("ClickOnce Publish Version: {0}", ver) End Sub End Module
Important
The Publish Version of an application for ClickOnce deployment is completely independent of its assembly version.
Comparing Version Objects
You can use the CompareTo method to determine whether one Version object is earlier than, the same as, or later than a second Version object. The following example indicates that Version 2.1 is later than Version 2.0.
Version v1 = new Version(2, 0);
Version v2 = new Version("2.1");
Console.Write("Version {0} is ", v1);
switch(v1.CompareTo(v2))
{
case 0:
Console.Write("the same as");
break;
case 1:
Console.Write("later than");
break;
case -1:
Console.Write("earlier than");
break;
}
Console.WriteLine(" Version {0}.", v2);
// The example displays the following output:
// Version 2.0 is earlier than Version 2.1.
open System
let v1 = Version(2, 0)
let v2 = Version "2.1"
printf $"Version {v1} is "
match v1.CompareTo v2 with
| 0 -> printf "the same as"
| 1 -> printf "later than"
| _ -> printf "earlier than"
printf $" Version {v2}."
// The example displays the following output:
// Version 2.0 is earlier than Version 2.1.
Dim v1 As New Version(2,0)
Dim v2 As New Version("2.1")
Console.Write("Version {0} is ", v1)
Select Case v1.CompareTo(v2)
Case 0
Console.Write("the same as")
Case 1
Console.Write("later than")
Case -1
Console.Write("earlier than")
End Select
Console.WriteLine(" Version {0}.", v2)
' The example displays the following output:
' Version 2.0 is earlier than Version 2.1.
For two versions to be equal, the major, minor, build, and revision numbers of the first Version object must be identical to those of the second Version object. If the build or revision number of a Version object is undefined, that Version object is considered to be earlier than a Version object whose build or revision number is equal to zero. The following example illustrates this by comparing three Version objects that have undefined version components.
using System;
enum VersionTime {Earlier = -1, Same = 0, Later = 1 };
public class Example
{
public static void Main()
{
Version v1 = new Version(1, 1);
Version v1a = new Version("1.1.0");
ShowRelationship(v1, v1a);
Version v1b = new Version(1, 1, 0, 0);
ShowRelationship(v1b, v1a);
}
private static void ShowRelationship(Version v1, Version v2)
{
Console.WriteLine("Relationship of {0} to {1}: {2}",
v1, v2, (VersionTime) v1.CompareTo(v2));
}
}
// The example displays the following output:
// Relationship of 1.1 to 1.1.0: Earlier
// Relationship of 1.1.0.0 to 1.1.0: Later
open System
type VersionTime =
| Earlier = -1
| Same = 0
| Later = 1
let showRelationship (v1: Version) (v2: Version) =
printfn $"Relationship of {v1} to {v2}: {v1.CompareTo v2 |> enum<VersionTime>}"
let v1 = Version(1, 1)
let v1a = Version "1.1.0"
showRelationship v1 v1a
let v1b = Version(1, 1, 0, 0)
showRelationship v1b v1a
// The example displays the following output:
// Relationship of 1.1 to 1.1.0: Earlier
// Relationship of 1.1.0.0 to 1.1.0: Later
Public Enum VersionTime
Earlier = -1
Same = 0
Later = 1
End Enum
Module Example
Public Sub Main()
Dim v1 As New Version(1, 1)
Dim v1a As New Version("1.1.0")
ShowRelationship(v1, v1a)
Dim v1b As New Version(1, 1, 0, 0)
ShowRelationship(v1b, v1a)
End Sub
Private Sub ShowRelationship(v1 As Version, v2 As Version)
Console.WriteLine("Relationship of {0} to {1}: {2}",
v1, v2, CType(v1.CompareTo(v2), VersionTime))
End Sub
End Module
' The example displays the following output:
' Relationship of 1.1 to 1.1.0: Earlier
' Relationship of 1.1.0.0 to 1.1.0: Later
Constructors
Version() |
Initializes a new instance of the Version class. |
Version(Int32, Int32) |
Initializes a new instance of the Version class using the specified major and minor values. |
Version(Int32, Int32, Int32) |
Initializes a new instance of the Version class using the specified major, minor, and build values. |
Version(Int32, Int32, Int32, Int32) |
Initializes a new instance of the Version class with the specified major, minor, build, and revision numbers. |
Version(String) |
Initializes a new instance of the Version class using the specified string. |
Properties
Build |
Gets the value of the build component of the version number for the current Version object. |
Major |
Gets the value of the major component of the version number for the current Version object. |
MajorRevision |
Gets the high 16 bits of the revision number. |
Minor |
Gets the value of the minor component of the version number for the current Version object. |
MinorRevision |
Gets the low 16 bits of the revision number. |
Revision |
Gets the value of the revision component of the version number for the current Version object. |
Methods
Clone() |
Returns a new Version object whose value is the same as the current Version object. |
CompareTo(Object) |
Compares the current Version object to a specified object and returns an indication of their relative values. |
CompareTo(Version) |
Compares the current Version object to a specified Version object and returns an indication of their relative values. |
Equals(Object) |
Returns a value indicating whether the current Version object is equal to a specified object. |
Equals(Version) |
Returns a value indicating whether the current Version object and a specified Version object represent the same value. |
GetHashCode() |
Returns a hash code for the current Version object. |
GetType() |
Gets the Type of the current instance. (Inherited from Object) |
MemberwiseClone() |
Creates a shallow copy of the current Object. (Inherited from Object) |
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. |
ToString() |
Converts the value of the current Version object to its equivalent String representation. |
ToString(Int32) |
Converts the value of the current Version object to its equivalent String representation. A specified count indicates the number of components to return. |
TryFormat(Span<Byte>, Int32) | |
TryFormat(Span<Byte>, Int32, Int32) | |
TryFormat(Span<Char>, Int32) |
Tries to format this version instance into a span of characters. |
TryFormat(Span<Char>, Int32, Int32) |
Tries to format this version instance into a span of characters. |
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) |
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. |
Operators
Equality(Version, Version) |
Determines whether two specified Version objects are equal. |
GreaterThan(Version, Version) |
Determines whether the first specified Version object is greater than the second specified Version object. |
GreaterThanOrEqual(Version, Version) |
Determines whether the first specified Version object is greater than or equal to the second specified Version object. |
Inequality(Version, Version) |
Determines whether two specified Version objects are not equal. |
LessThan(Version, Version) |
Determines whether the first specified Version object is less than the second specified Version object. |
LessThanOrEqual(Version, Version) |
Determines whether the first specified Version object is less than or equal to the second Version object. |
Explicit Interface Implementations
IComparable.CompareTo(Object) |
Compares the current Version object to a specified object and returns an indication of their relative values. |
IFormattable.ToString(String, IFormatProvider) |
Formats the value of the current instance using the specified format. |
ISpanFormattable.TryFormat(Span<Char>, Int32, ReadOnlySpan<Char>, IFormatProvider) |
Tries to format the value of the current instance into the provided span of characters. |
IUtf8SpanFormattable.TryFormat(Span<Byte>, Int32, ReadOnlySpan<Char>, IFormatProvider) |