简体   繁体   English

为什么.NET System.Version "2.0" 与 "2.0.0.0" 不同?

[英]Why is .NET System.Version "2.0" different from "2.0.0.0"?

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.我理解它背后的实现机制,因为最后两位数字被初始化为-1,-1小于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.从数学的角度来看,无论我附加多少个零,1 和 1.0 和 1.000000 之间都没有区别。

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.构造函数的第三个参数是内部版本号,如果在构造 Version 对象时未分配,则将其初始化为 -1。 Conceivably a Version object could be created before an assembly or assemblies is built for the first time eg in some build pipeline logic.可以想象,可以在首次构建一个或多个程序集之前创建 Version 对象,例如在某些构建管道逻辑中。 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 (ie whether one Version predates or postdates another) a version which has never been built or revised "is less than" a version which has.由于“大于”和“小于”运算符重载实际上是关于确定构建的相对时间(即一个版本是否早于或晚于另一个版本)从未构建或修订的版本“小于”具有.

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.*.* .从这个意义上说, 2.0代表任何2.0导数,或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 .当您将关系运算符与版本一起使用时,您实际上想要回答兼容性问题,其中>=表示is backwards compatible with .

So, V1 >= V2 would mean is V1 backwards compatible with V2 .因此, V1 >= V2意味着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.0.0 >= 2.0.*.* -> true,因为2.0.0.0应该能够在支持声称支持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 2.0.*.* >= 2.0.0.0 -> false,因为并非每个2.0.*.*版本都保证与2.0.0.0兼容

[EDIT: answering the comment] [编辑:回答评论]

shouldn't then 2.0.*.* at least be equal to 2.0.0.0 ?那么2.0.*.*不应该至少等于2.0.0.0吗? Because (in your words) " 2.0.0.0 should be able to run on system supporting claiming to support 2.0.*.* "因为(用你的话来说)“ 2.0.0.0应该能够在支持声称支持2.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.我认为让您感到困惑的是我们选择了2.0.0.0 ,它直观地理解为基本版本,因此在逻辑上等同于2.0.*.* ,但事实并非如此。

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.它不应该相等,因为2.0.*.*表示任何2.0 版本(不仅仅是被选中的特定版本),因此ANY_20_VERSION == 2000_VERSION是假的。 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换句话说,这意味着任何2.0导数都应该能够满足关系(不仅仅是被选中的特定的),显然2.0.0.12.0.0.0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 System.Version未序列化 - System.Version not serialized 为什么JSON字符串中的System.Version没有正确反序列化? - Why System.Version in JSON string does not deserialize correctly? 无法将类型'System.Version'隐式转换为'System.Net.HttpVersion' - Cannot implicitly convert type 'System.Version' to 'System.Net.HttpVersion' 使用System.Version进行通用版本控制 - Using System.Version for general purpose versioning sql server中System.Version的数据类型 - Datatype for System.Version in sql server System.Version 的序列化和反序列化使用 Newtonsoft,dotnet core 2.2 vs dot net core 3.1 - Serialization and Deserialization of System.Version using Newtonsoft, dotnet core 2.2 vs dot net core 3.1 使用被套。 Net Framework 生成错误:无法评估表达式“[System.Version]::Parse('')” - Using coverlet with. Net Framework generates an error: The expression "[System.Version]::Parse('')" canot be evaluated 如何防止System.Version删除前导零? - How can I prevent System.Version from removing leading zeroes? 使用Entity Framework Code First将System.Version存储在数据库中 - Storing System.Version in database with Entity Framework Code First system.version超过3个小数点C# - system.version more than 3 decimal points c#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM