What is the purpose of Decimal.One, Decimal.Zero, Decimal.MinusOne in .Net

A simple question: why does the decimal type define these constants? Why bother?

I am looking for the reason why this is determined by the language, and it is not possible to use or effects for the compiler. Why put this first? The compiler can embed 0m just as easily as Decimal.Zero, so I don't buy it as a compiler shortcut.

+57
decimal c # constants
Apr 13 '09 at 22:16
source share
4 answers

A little clarification. They are actually readonly static values, not constants. This makes a clear difference in .Net, because constant values ​​are built into different compilers and therefore it is not possible to track their use in the compiled assembly. The readonly static values, however, are not copied, but referenced. This is useful for your question, because it means that their use can be analyzed.

If you use a reflector and dig up BCL, you will notice that MinusOne and Zero are only used in the VB runtime. It exists primarily to serve conversions between decimal and boolean values. Why is MinusOne used, coincidentally, appeared on a separate thread only today ( link )

Oddly enough, if you look at the Decimal.One value, you will notice that it is not used anywhere.

As to why they are clearly defined ... I doubt there is a solid and quick reason. It appears that there is no specific performance, and only a small part of the convenience that can be attributed to their existence. I assume that they were added by someone during the development of BCL for their convenience and are simply not removed.

EDIT

Obeyed in const more detail after the comment from @Paleta. The C # Decimal.One uses the const modifier, however it is emitted as static readonly at the IL level. The C # compiler uses several tricks to make this value practically indistinguishable from const (for example, in literals). This will be displayed in a language that recognizes this trick (VB.Net recognizes this, but F # does not).

+27
Apr 13 '09 at 10:30
source share

Some .NET languages ​​do not support the decimal data type, and in these cases it is more convenient (and faster) to write Decimal.ONE instead of the new decimal number (1).

The Java BigInteger class has ZERO and ONE, for the same reason.

+20
Apr 13 '09 at 22:26
source share

My opinion is that they help to avoid magic numbers.

Magic numbers are mostly in your code, where you have an arbitrary number floating around. For example:

 int i = 32; 

This is problematic in the sense that no one can understand why I am assigned the value 32, or what it means 32, or if it should be 32 at all. It is magical and mysterious.

In the same vein, I often see code that does this.

 int i = 0; int z = -1; 

Why are they set to 0 and -1? Is this just a coincidence? Do they mean something? Who knows?

While Decimal.One , Decimal.Zero , etc. They don’t tell you what the values ​​mean in the context of your application (perhaps zero means “absent”, etc.), it tells you that the value was intentionally set, and probably has some meaning.

Although not ideal, it is much better than not saying anything at all :-)

Note This is not for optimization. Observe this C # code:

 public static Decimal d = 0M; public static Decimal dZero = Decimal.Zero; 

When viewing the generated bytecode using ildasm, both parameters result in an identical MSIL. System.Decimal is a value type, so Decimal.Zero no more "optimal" than just using a literal value.

-3
Apr 13 '09 at 22:23
source share

Those are 3 arghhh values ​​!!!

I think they may have something to do with what I call trailing 1

let's say you have this formula:

(x) 1,116666 + (y) = (z) 2,00,000

but x , z are rounded to 0.11 and 2.00 , and you will be asked to calculate (y).

so you can think of y = 2.00 - 1.11 . Actually y is 0.88 , but you get 0.89 . (difference of 0.01 ).

It depends on the actual value of x and y, the results will vary from -0.01 to +0.01 , and in some cases when working with a bunch of those, and for the convenience of things, you can check whether the final value Decimal.MinusOne / 100 , Decimal.One / 100 or Decimal.Zero / 100 to fix them.

this is how i used them.

-four
Aug 02 '10 at 23:38
source share



All Articles