PDA

View Full Version : Static decimal members



thunderlizard
13th November 2009, 15:37
Obscure bit of .NET that I hadn't noticed in my many years of it:
there are static members for decimal.One, decimal.Zero and decimal.MinusOne.

Prudent of Microsoft to have reserved themselves the ability to adopt their own definitions of these numbers in future: but if they want to change any other numbers they're stuck.

StackOverflow is normally great, but it struggles with this one:
http://stackoverflow.com/questions/745591/what-is-the-purpose-of-decimal-one-decimal-zero-decimal-minusone-in-c

DimPrawn
13th November 2009, 15:41
Obscure bit of .NET that I hadn't noticed in my many years of it:
there are static members for decimal.One, decimal.Zero and decimal.MinusOne.

Prudent of Microsoft to have reserved themselves the ability to adopt their own definitions of these numbers in future: but if they want to change any other numbers they're stuck.

StackOverflow is normally great, but it struggles with this one:
http://stackoverflow.com/questions/745591/what-is-the-purpose-of-decimal-one-decimal-zero-decimal-minusone-in-c

Don't tell me you've been using the plain vanilla 0,1 and -1 in your code!

It's a bit like "" vs string.Empty

It's readability for squinty eyed techies. :nerd

thunderlizard
13th November 2009, 15:44
I know, I've been using "magic numbers" all this time and am heartily sorry!

From here on in, it's going to be:


for(decimal d=decimal.Zero; d<things.Count; d+=decimal.One)
{
}

Diestl
13th November 2009, 15:47
So they can at a later date put a better optimized 1 in place and you wont have to change your code.

thunderlizard
13th November 2009, 15:51
You think so? That is as plausible as anything I suppose. But something tells me that the people worried about the performance of their 1's aren't using .NET in the first place.

NickFitz
13th November 2009, 15:54
Forth (FORTH if you prefer) used to define 0, 1, 2, -1 and -2 as constants - it could be a win for an indirect-threaded implementation running on something like a 6809 or a Z80.

Of course, there was nothing to stop you redefining them:



: 0 ( - n) ." Hello World" 3 ;

: 1 ( - n) CAKE BAKE 42 ;

: -1 ( - n) R> DROP [ BASE @ DECIMAL 36 BASE ! ] FAIL [ BASE ! ] ;