Andrej Tozon's blog

In the Attic

NAVIGATION - SEARCH

Versioning with Version class

Sometimes you need to provide versioning functionality for your custom data, databases, etc. For handling version information inside your application you can use data types like string or int, or go with the Version class, which you commonly use when managing application and assembly versions.
When implementing custom versioning, which strongly depended on version comparison, I first had some doubts about using Version class for that. But this was mainly because of its serialized representation as a string ("1.0.0.9"), since strings containing numeric values still compare (and sort, for that matter) like strings. For example:

string s9 = "1.0.0.9";
string s10 = "1.0.0.10";
string s11 = "1.0.0.11";

Console.WriteLine(string.Compare(s11, s10) > 0);
Console.WriteLine(string.Compare(s10, s9) > 0);

The first WriteLine statement would write "True", since strings "1.0.0.10" and "1.0.0.11" are equal in length and the latter is obviously greater than the first one. But when comparing the second pair, the result would be "False", like "1.0.0.10" is less than "1.0.0.9". That's because string comparison is made on a character base, not numeric. And character '9' in 7th position of s9 string evaluates as greater than '1' in the same position of the second one. The last zero in s10 just doesn't make any difference.

So I tried Version:

Version v9 = new Version("1.0.0.9");
Version v10 = new Version("1.0.0.10");
Version v11 = new Version("1.0.0.11");

Console.WriteLine(v11 > v10);
Console.WriteLine(v10 > v9);

Version class comparison works as expected and both statements would write out the value of True.

The point here would be - don't judge objects for their serialized look.