Andrej Tozon's blog

In the Attic


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 (""), since strings containing numeric values still compare (and sort, for that matter) like strings. For example:

string s9 = "";
string s10 = "";
string s11 = "";

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

The first WriteLine statement would write "True", since strings "" and "" 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 "" is less than "". 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("");
Version v10 = new Version("");
Version v11 = new Version("");

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.