/// <summary>
/// Version
/// </summary>
public class Version : IComparable<Version>
{
/// <summary>
/// Major version
/// </summary>
public readonly int Major = 0;
/// <summary>
/// Minor version
/// </summary>
public readonly int Minor = 0;
/// <summary>
/// Build number
/// </summary>
public readonly int Build = 0;
/// <summary>
/// Revision
/// </summary>
public readonly int Revision = 0;
private readonly int hash;
/// <summary>
/// Version
/// </summary>
/// <param name="version"></param>
public Version(string version)
{
string[] versions = version.Split('.');
if (versions == null)
{
throw new Exception("Can't parse version from string: " + version);
}
if (versions.Length != 4)
{
throw new Exception("Can't parse version from string — wrong count of numbers: " + version);
}
if (int.TryParse(versions[0], out int major))
{
Major = major;
}
else
{
throw new Exception("Can't parse major version from string: " + versions[0]);
}
if (int.TryParse(versions[1], out int minor))
{
Minor = minor;
}
else
{
throw new Exception("Can't parse minor version from string: " + versions[1]);
}
if (int.TryParse(versions[2], out int build))
{
Build = build;
}
else
{
throw new Exception("Can't parse build version from string: " + versions[2]);
}
if (int.TryParse(versions[3], out int revisison))
{
Revision = revisison;
}
else
{
throw new Exception("Can't parse revisison version from string: " + versions[3]);
}
hash = ToString().GetHashCode();
}
/// <summary>
/// Versions compare
/// </summary>
/// <param name="other"></param>
/// <returns></returns>
public int CompareTo(Version other)
{
if (Major > other.Major) { return 1; }
if (Major < other.Major) { return -1; }
// Major == other.Major
if (Minor > other.Minor) { return 1; }
if (Minor < other.Minor) { return -1; }
// Minor == other.Minor
if (Build > other.Build) { return 1; }
if (Build < other.Build) { return -1; }
// Build == other.Build
if (Revision > other.Revision) { return 1; }
if (Revision < other.Revision) { return -1; }
// Revision == other.Revision
return 0;
}
/// <summary>
/// Bigger?
/// </summary>
/// <param name="left"></param>
/// <param name="right"></param>
/// <returns></returns>
public static bool operator >(Version left, Version right)
{
return left.CompareTo(right) == 1;
}
/// <summary>
/// Less?
/// </summary>
/// <param name="left"></param>
/// <param name="right"></param>
/// <returns></returns>
public static bool operator <(Version left, Version right)
{
return left.CompareTo(right) == -1;
}
/// <summary>
/// Bigger or equal?
/// </summary>
/// <param name="left"></param>
/// <param name="right"></param>
/// <returns></returns>
public static bool operator >=(Version left, Version right)
{
return left.CompareTo(right) >= 0;
}
/// <summary>
/// Less or equal?
/// </summary>
/// <param name="left"></param>
/// <param name="right"></param>
/// <returns></returns>
public static bool operator <=(Version left, Version right)
{
return left.CompareTo(right) <= 0;
}
/// <summary>
/// Convert version to string
/// </summary>
/// <returns></returns>
public override string ToString()
{
return string.Format("{0}.{1}.{2}.{3}", Major, Minor, Build, Revision);
}
/// <summary>
/// Get hash
/// </summary>
/// <returns></returns>
public override int GetHashCode()
{
return hash;
}
}