Version class in C#

gpeipman
1,053 views

Open Source Your Knowledge, Become a Contributor

Technology knowledge has to be shared and made accessible for free. Join the movement.

Create Content

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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.

1
2
3
4
5
6
7
8
9
using System;
class Hello
{
static void Main()
{
Console.WriteLine(new Version("1.0"));
}
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

But what if we give only major version?

1
2
3
4
5
6
7
8
9
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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

Open Source Your Knowledge: become a Contributor and help others learn. Create New Content