C# .Net 中的 Decimal.One、Decimal.Zero、Decimal.MinusOne 的目的是什么
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/745591/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the purpose of Decimal.One, Decimal.Zero, Decimal.MinusOne in .Net
提问by Jasmine
Simple question - why does the Decimal type define these constants? Why bother?
简单的问题 - 为什么 Decimal 类型定义这些常量?何苦?
I'm looking for a reason why this is defined by the language, not possible uses or effects on the compiler. Why put this in there in the first place? The compiler can just as easily in-line 0m as it could Decimal.Zero, so I'm not buying it as a compiler shortcut.
我正在寻找一个由语言定义的原因,而不是对编译器的可能用途或影响。为什么要把它放在首位?编译器可以像 Decimal.Zero 一样轻松地将 0m 内联,所以我不会将其作为编译器快捷方式购买。
采纳答案by JaredPar
Small clarification. They are actually static readonly values and not constants. That has a distinct difference in .Net because constant values are inlinedby the various compilers and hence it's impossible to track their usage in a compiled assembly. Static readonly values however are not copied but instead referenced. This is advantageous to your question because it means the use of them can be analyzed.
小澄清。它们实际上是静态只读值而不是常量。这在 .Net 中有明显的不同,因为常量值由各种编译器内联,因此不可能在编译的程序集中跟踪它们的使用情况。然而,静态只读值不会被复制而是被引用。这对您的问题有利,因为这意味着可以分析它们的使用。
If you use reflector and dig through the BCL, you'll notice that MinusOne and Zero are only used with in the VB runtime. It exists primarily to serve conversions between Decimal and Boolean values. Why MinusOne is used coincidentally came up on a separate thread just today (link)
如果您使用反射器并深入 BCL,您会注意到 MinusOne 和 Zero 仅在 VB 运行时中使用。它的存在主要是为了提供 Decimal 和 Boolean 值之间的转换。为什么使用 MinusOne 巧合的是今天在一个单独的线程上出现(链接)
Oddly enough, if you look at the Decimal.One value you'll notice it's used nowhere.
奇怪的是,如果您查看 Decimal.One 值,您会注意到它在任何地方都没有使用。
As to why they are explicitly defined ... I doubt there is a hard and fast reason. There appearsto be no specific performance and only a bit of a convenience measure that can be attributed to their existence. My guessis that they were added by someone during the development of the BCL for their convenience and just never removed.
至于为什么明确定义它们......我怀疑有一个硬性的原因。有似乎是没有具体表现,只有一点是可以归因于他们的存在便利措施。我的猜测是它们是在 BCL 开发过程中为方便起见被某人添加的,并且从未被删除。
EDIT
编辑
Dug into the const
issue a bit more after a comment by @Paleta. The C# definition of Decimal.One
uses the const
modifier however it is emitted as a static readonly
at the IL level. The C# compiler uses a couple of tricks to make this value virtually indistinguishable from a const
(inlines literals for example). This would show up in a language which recognize this trick (VB.Net recognizes this but F# does not).
const
在@Paleta 发表评论后,更深入地研究了这个问题。C# 定义Decimal.One
使用const
修饰符,但它static readonly
在 IL 级别作为 a 发出。C# 编译器使用了一些技巧来使这个值与 a const
(例如内联文字)几乎没有区别。这将显示在识别此技巧的语言中(VB.Net 识别此但 F# 不识别)。
回答by Orion Edwards
My opinionon it is that they are there to help avoid magic numbers.
我对此的看法是,它们的存在是为了帮助避免幻数。
Magic numbers are basically anywhere in your code that you have an aribtrary number floating around. For example:
幻数基本上位于您的代码中任意浮动的数字的任何位置。例如:
int i = 32;
This is problematic in the sense that nobody can tell whyi is getting set to 32, or what 32 signifies, or if it should be 32 at all. It's magical and mysterious.
这是有问题的,因为没有人知道为什么我被设置为 32,或者 32 意味着什么,或者它是否应该是 32。它神奇而神秘。
In a similar vein, I'll often see code that does this
同样,我会经常看到执行此操作的代码
int i = 0;
int z = -1;
Why are they being set to 0 and -1? Is this just coincidence? Do they mean something? Who knows?
为什么它们被设置为 0 和 -1?这只是巧合吗?他们有什么意思吗?谁知道?
While Decimal.One
, Decimal.Zero
, etc don't tell you what the values mean in the context of your application (maybe zero means "missing", etc), it doestell you that the value has been deliberately set, and likely has some meaning.
虽然Decimal.One
,Decimal.Zero
等不会告诉您这些值在您的应用程序上下文中的含义(可能零表示“缺失”等),但它确实告诉您该值是有意设置的,并且可能具有某种含义。
While not perfect, this is much better than not telling you anything at all :-)
虽然不完美,但这比什么都不告诉你要好得多:-)
NoteIt's notfor optimization. Observe this C# code:
注意这不是为了优化。观察这个 C# 代码:
public static Decimal d = 0M;
public static Decimal dZero = Decimal.Zero;
When looking at the generated bytecode using ildasm, both options result in identicalMSIL. System.Decimal
is a value type, so Decimal.Zero
is no more "optimal" than just using a literal value.
使用 ildasm 查看生成的字节码时,两个选项都会产生相同的MSIL。System.Decimal
是一种值类型,因此Decimal.Zero
并不比仅使用文字值更“最佳”。
回答by mihi
Some .NET languages do not support decimal as a datatype, and it is more convenient (and faster) in these cases to write Decimal.ONE instead of new Decimal(1).
某些 .NET 语言不支持十进制作为数据类型,在这些情况下,编写 Decimal.ONE 而不是 new Decimal(1) 更方便(也更快)。
Java's BigInteger class has ZERO and ONE as well, for the same reason.
出于同样的原因,Java 的 BigInteger 类也有零和一。
回答by Hassen
Those 3 values arghhh !!!
这三个值啊啊啊!!!
I think they may have something to do with what I call trailing 1's
我认为它们可能与我所说的尾随 1 有关
say you have this formula :
说你有这个公式:
(x)1.116666 + (y) = (z)2.00000
(x)1.116666 + (y) = (z)2.00000
but x, zare rounded to 0.11and 2.00, and you are asked to calculate (y).
但是x, z被四舍五入为0.11和2.00,并且要求您计算 (y)。
so you may think y = 2.00 - 1.11
. Actually yequals to 0.88but you will get 0.89. (there is a 0.01in difference).
所以你可能会想y = 2.00 - 1.11
。实际上y等于0.88但你会得到0.89。(相差0.01)。
Depends on the real value of x and y the results will varies from -0.01to +0.01, and in some cases when dealing with a bunch of those trailing 1's, and to facilate things, you can check if the trailing value equals to Decimal.MinusOne / 100
, Decimal.One / 100
or Decimal.Zero / 100
to fix them.
取决于 x 和 y 的实际值,结果将在-0.01到+0.01之间变化,在某些情况下,在处理一堆尾随 1 时,为了方便处理,您可以检查尾随值是否等于Decimal.MinusOne / 100
,Decimal.One / 100
或Decimal.Zero / 100
修复它们。
this is how i've made use of them.
这就是我如何利用它们。