Reputation: 1843
I recently ran into a snag when this compare failed:
System.Version compareVersion = new Version(2, 0, 0, 0);
System.Version actualVersion = new Version(2, 0);
if(actualVersion >= compareVersion) // returns false
I understand the implementation mechanics behind it, because the last two digits are initialized as -1 and -1 is less than 0.
But: What's the rationale behind it? From a mathematical standpoint there is no difference between 1 and 1.0 and 1.000000 no matter how many zeros I append.
Upvotes: 2
Views: 185
Reputation: 14581
Here's my take on it. I might be completely wrong, but as you are asking for rationale, this is how I see version numbers and their use.
Version numbers are not actually numbers, they are really just identifiers with some semantics and comparison logic based on that semantics. Part of that logic is being able to check whether two versions are compatible.
In that sense, 2.0
represents any 2.0
derivative, or 2.0.*.*
.
When you use relational operators with versions, you actually want to answer the compatibility question, where >=
means something like is backwards compatible with
.
So, V1 >= V2
would mean is V1 backwards compatible with V2
.
2.0.0.0 >= 2.0.*.*
-> true, as 2.0.0.0
should be able to run on system supporting claiming to support 2.0.*.*
2.0.*.* >= 2.0.0.0
-> false, as not every 2.0.*.*
version is guaranteed to be compatible with 2.0.0.0
[EDIT: answering the comment]
shouldn't then
2.0.*.*
at least be equal to2.0.0.0
? Because (in your words) "2.0.0.0
should be able to run on system supporting claiming to support2.0.*.*
"
I think what's confusing you is that we picked 2.0.0.0
which is intuitivelly understood as base version, thus logically equivalent to 2.0.*.*
, but it isn't.
It shouldn't be equal, as 2.0.*.*
means any 2.0 version (not just specific one that was picked), thus ANY_20_VERSION == 2000_VERSION
is false. In other words, this would mean that any 2.0
derivative should be able to satisfy the relation (not just specific one that was picked), and obviously 2.0.0.1
is not same as 2.0.0.0
Upvotes: 1
Reputation: 91
As per the docs
"A difference in build number represents a recompilation of the same source. Different build numbers might be used when the processor, platform, or compiler changes."
The third argument to the constructor is the build number which gets initialised to -1 if not assigned when the Version object is constructed. Conceivably a Version object could be created before an assembly or assemblies is built for the first time e.g. in some build pipeline logic. Once the assemblies are built and a build number exists a new Version object with build number would succeed the Version without.
Since the "greater than" and "less than" operator overloads are really about determining the relative times of builds (i.e whether one Version predates or postdates another) a version which has never been built or revised "is less than" a version which has.
Upvotes: 1