Skip to content
Tutorials Center
My learning

Topics

C# Reference ยท Types and Literals

The Version class

Reference for System.Version in C#: major, minor, build and revision, parsing, comparing, ToString field counts and why it is not SemVer.

System.Version represents a dotted version number with two to four numeric parts: Major.Minor, Major.Minor.Build or Major.Minor.Build.Revision. It is the type behind assembly versions, Environment.Version and OS version checks. Its main value is correct comparison: 1.10 is newer than 1.9, which string comparison gets wrong.

Version is a class, immutable, and implements IComparable<Version>, so it sorts and compares with <, > and ==.

C#
var v = new Version(2, 10, 1);
Console.WriteLine(v > new Version(2, 9));

Try it

C# Parsing and comparing

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

C# Missing parts and ToString

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

Syntax

C#
public sealed class Version : ICloneable, IComparable, IComparable<Version>, IEquatable<Version>, ISpanFormattable
public Version(int major, int minor)
public Version(int major, int minor, int build)
public Version(int major, int minor, int build, int revision)
public Version(string version)
Member Description
Major, Minor Always present.
Build, Revision -1 when the version was created without them.
Parse, TryParse From text such as "1.2.3"; two to four numeric parts.
ToString(), ToString(fieldCount) All defined parts, or the first N.
CompareTo, Equals, operators Compare part by part.

Errors: negative numbers throw ArgumentOutOfRangeException; invalid text throws FormatException (or ArgumentException for the wrong number of parts); ToString(n) throws ArgumentException if n is more than the defined parts.

Usage notes

  • Undefined parts are -1 and sort lower. 1.2 is less than 1.2.0, and the two are not equal. Normalize versions to the same number of parts before comparing, or compare only the parts you care about.
  • It is not Semantic Versioning. There is no pre-release label such as -beta and no build metadata. Version.TryParse("1.2-beta") fails. Use a SemVer library or your own type if you need that.
  • Parts are int. Date-style versions like 2025.10.1 fit; very long numeric parts do not.
  • Common sources. Assembly.GetName().Version, Environment.Version (the runtime) and Environment.OSVersion.Version.
  • Null. == handles null on either side; CompareTo(null) treats the current instance as larger.
  • Stable hashing. Version can be a dictionary key.
  • Display. ToString() prints only defined parts, so new Version(1, 2) prints 1.2.