Back
Close

Version class in C#

gpeipman
2,046 views

Version class

There is built-in Version class available in .NET and it is easy to use in C#. Version class supports major, minor, build and revision numbers as integers. Class cam be initialized from string or with some combination of versions. Here are class constructors.

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

Initialization from version number parts is self describing. Let's see how to initialize version from string.

using System;
class Hello
{
static void Main()
{
var versions = new [] {
new Version("1.0.0.0"),
new Version("2.1.0.0"),
new Version("3.1.2.0"),
new Version("2.3.1.5")
};
foreach(var version in versions)
{
Console.WriteLine(version);
}
}
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Now let's try out how intelligent Version class is and let's expect that missing version parts in version string are considered as zeros.

using System;
class Hello
{
static void Main()
{
Console.WriteLine(new Version("1.0"));
}
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

But what if we give only major version?

using System;
class Hello
{
static void Main()
{
Console.WriteLine(new Version("1"));
}
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

This is the situation that Version class cannot handle. There must be major and minor versions. Minor version can be zero but it must be given when version is parsed from string.

Using Version class in code

Now let's see how to use Version class in real code. Often we cannot make application throw exceptions to user when we can avoid it and clearly communicate the problem to user.

using System;
class Hello
{
static void Main()
{
Version version;
if(!Version.TryParse("1", out version))
{
Console.WriteLine("Version not parsed");
}
else
{
Console.WriteLine("Version parsed");
}
}
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

If we have more logic needed or we want to keep application architecture clean then we can also create Version factory.

using System;
class Hello
{
static void Main()
{
Version version1 = VersionFactory.CreateVersion("1");
Version version2 = VersionFactory.CreateVersion("1.2");
Console.WriteLine("version1 = " + (version1 == null ? "null" : version1.ToString()));
Console.WriteLine("version2 = " + (version2 == null ? "null" : version2.ToString()));
}
}
public static class VersionFactory
{
public static Version CreateVersion(string versionString)
{
Version version;
if(!Version.TryParse(versionString, out version))
{
return null;
}
return version;
}
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

We can add more logic to VersionFactory class if needed.

References

Create your playground on Tech.io
This playground was created on Tech.io, our hands-on, knowledge-sharing platform for developers.
Go to tech.io
codingame x discord
Join the CodinGame community on Discord to chat about puzzle contributions, challenges, streams, blog articles - all that good stuff!
JOIN US ON DISCORD
Online Participants