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
("126.96.36.199"), since strings containing numeric values still compare (and
sort, for that matter) like strings. For example:
string s9 = "188.8.131.52";
string s10 = "184.108.40.206";
string s11 = "220.127.116.11";
Console.WriteLine(string.Compare(s11, s10) > 0);
Console.WriteLine(string.Compare(s10, s9) > 0);
The first WriteLine statement would write "True", since strings
"18.104.22.168" and "22.214.171.124" 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 "126.96.36.199" is less than
"188.8.131.52". 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("184.108.40.206");
Version v10 = new Version("220.127.116.11");
Version v11 = new Version("18.104.22.168");
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.