Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a zero [ 0 ] constant somewhere in any Microsoft .NET class?

I'm just curious and I know it's not of much value, but here it goes...

I think that I have seen something like that somewhere but I'm not sure.

I mean something like this:

var zero = Class.Zero;

I tried looking at the Math class but it's not there.

I also know that I can use an unsigned value type like ushort.Min to get a Zero ( 0 ) value; it's not what I'm asking here... :D

like image 936
Leniel Maccaferri Avatar asked Apr 25 '11 15:04

Leniel Maccaferri


People also ask

Does C# have constants?

Constants are declared with the const modifier. Only the C# built-in types (excluding System. Object) may be declared as const . User-defined types, including classes, structs, and arrays, cannot be const .

How do you keep a constant in C#?

Syntax of Constant in C# We have to use "const" keyword to declare constant variable. Constants are effectively static because the value of the constant is the same in all instances of the class but you can declare constants without a static keyword an example is shown below. Display Output : PI Value is : 3.14.

Is .NET framework obsolete?

NET framework is highly outdated. Of course, the main reason behind this is the lack of cross-platform development, which was rectified in future versions released by Microsoft. Most developers and programmers prefer programming languages and frameworks that offer flexibility when working with them.

How do you define something in C#?

#define lets you define a symbol. By using the symbol as the expression passed to the #if directive, the expression evaluates to true . You can also define a symbol with the DefineConstants compiler option. You can undefine a symbol with #undef .


2 Answers

Do you mean default(T)?

int zero = default(int);

This represents the default value for a given type, for int this is 0. You should not use this if you know already you need zero though, only in the case that you have a type at run time for which you need the default value.

like image 184
BrokenGlass Avatar answered Oct 04 '22 03:10

BrokenGlass


There's one for Decimal.Zero and a few other more complex types like TimeSpan.Zero, IntPtr.Zero and BigInteger.Zero. But for regular numeric types, just use 0.

like image 34
Daniel Renshaw Avatar answered Oct 04 '22 03:10

Daniel Renshaw