Using Version Class

When dealing with version numbers we often need to convert them to string and vice versa. There is lot of code where versions are handled manually in code. I don’t know why. But I know for sure there is class called Version and I’m sure this class will help us a lot. Let’s see a little example.

The following code:

Version ver = new Version("1.4");
Console.WriteLine("Major: " + ver.Major.ToString());
Console.WriteLine("Minor: " + ver.Minor.ToString());
Console.WriteLine("String: " + ver.ToString());

outputs something like this:

Major: 1

Minor: 4

String: 1.4

As we can see, this class makes using versions very easy. There is one nasty limitation – Version class is sealed. If there is need for non-sealed class then you should write it by yourself.

Gunnar Peipman

Gunnar Peipman is ASP.NET, Azure and SharePoint fan, Estonian Microsoft user group leader, blogger, conference speaker, teacher, and tech maniac. Since 2008 he is Microsoft MVP specialized on ASP.NET.

    2 thoughts on “Using Version Class

    Leave a Reply

    Your email address will not be published. Required fields are marked *