Decimal.One,Decimal.Zero,Decimal.MinusOne在.NET中的作用是什么 [英] What is the purpose of Decimal.One, Decimal.Zero, Decimal.MinusOne in .Net

查看:283
本文介绍了Decimal.One,Decimal.Zero,Decimal.MinusOne在.NET中的作用是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简单的问题 - 为什么十进制类型定义这些常量?何必呢?

Simple question - why does the Decimal type define these constants? Why bother?

我在寻找原因,这是由语言,编译器无法使用或效果定义的一个原因。为什么在首位把这个在那里?编译器可以很容易在网上0米,因为它可以Decimal.Zero,所以我不买它作为一个编译器的快捷方式。

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.

推荐答案

小澄清。他们实际上是静态只读值,而不是常量。这已在.net中的一个明显区别,因为常数值的内联的由各种编译器,因此它无法跟踪其使用在编译的程序集。静态只读值但不被复制,而是引用。这有利于你的问题,因为这意味着使用它们进行分析。

Small clarification. They are actually static readonly values and not constants. That has a distinct difference in .Net because constant values are inlined by 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.

如果您使用反射器,并通过挖掘首创置业,你会发现MinusOne和零只用在VB运行时使用。它的存在主要是为十进制和布尔值之间的转换。为什么MinusOne用于巧合的是就在今天(想出了一个单独的线程<一href="http://stackoverflow.com/questions/745292/convert-boolean-to-integer-in-vb-net/745366#745366">link)

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)

奇怪的是,如果你看一下Decimal.One值,你会发现它无处使用。

Oddly enough, if you look at the Decimal.One value you'll notice it's used nowhere.

至于为什么它们被明确定义......我怀疑是有硬性的原因。这里的显示是没有具体表现,只有一点是可以归因于他们的存在方便的考虑的。我的猜到的是,他们是被人首创置业的发展,为他们的方便时加入,只是永远不会被删除。

As to why they are explicitly defined ... I doubt there is a hard and fast reason. There appears to be no specific performance and only a bit of a convenience measure that can be attributed to their existence. My guess is that they were added by someone during the development of the BCL for their convenience and just never removed.

修改

挖成常量发出多一点评论由@Paleta后。 C#的定义 Decimal.One 使用常量修改但它是发出一个静只读在IL水平。 C#编译器使用了几个技巧,使这个值实际上从常量区分(内联文本为例)。这会出现在其中承认这一招语言(VB.Net认识到这一点,但F#不)。

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).

这篇关于Decimal.One,Decimal.Zero,Decimal.MinusOne在.NET中的作用是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆