Thursday, October 20, 2011

Comparing Versions in .NET

A quick note when comparing System.Version objects. Looking at this expression, my guess would be that it is true, but it is not..
new Version("9.1") == new Version("9.1.0.0") // FALSE

What’s more weird is that this expression below is true!
new Version("9.1") < new Version("9.1.0.0") // TRUE!
And that’s because unspecified components of the version are set to –1. In this case the build and revision for the Version on the left-hand side is –1.

So if you plan to use short-form versions like “9.1” you have to be careful when comparing it with full versions of the form “9.1.3”. This is specially important when one of the versions is constructed dynamically (user input or by querying some database).

No comments:

Post a Comment