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

I'm just curious, and I know that this is not very valuable, but here it is ...

I think I saw something like this somewhere, but I'm not sure.

I mean something like this:

var zero = Class.Zero; 

I tried to take a look at the Math class, but it is not there.

I also know that I can use the unsigned value type, for example ushort.Min , to get the value Zero (0); this is not what I ask here ...: D

+6
source share
4 answers

There is one for Decimal.Zero and several other more complex types such as TimeSpan.Zero , IntPtr.Zero and BigInteger.Zero . But for regular numeric types, just use 0 .

+10
source

Do you mean default(T )?

 int zero = default(int); 

This is the default value for this type, for int it is 0. You should not use this if you know that you already need zero, but only if you have a type at runtime for which you need a default value .

+10
source

The .Net Framework does not define constants for values ​​such as 0 . If you want to use 0 , just use 0

Certain numerical constants in the .Net Framework usually revolve around the limits of a given numerical type, values ​​that are of particular relevance, or when a null value requires special / complex initialization. for instance

  • Int32.Max
  • Int32.Min
  • Double.NaN

Literal 0 not suitable for these categories for most numeric types ( Decimal is one exception)

+5
source

Some immutable reference types have predefined instances for null values. For example, String defines String.Empty. This is because the actual string — even one without characters — must refer to the actual heap object, but if there are a thousand empty String variables, they can all refer to the same heap object. If the application does not use any empty strings at all, creating one instance of the empty string at application startup and sharing it with everyone who needs an empty string will be more efficient than creating a new empty string object each time one is needed.

Such a value will not exist with type values. Although there are certain constants of type values ​​declared (for example, Math.Pi), declaring them for convenience rather than efficiency. Saying "myDouble = Math.Pi" is no more effective than "MyDouble = 3.141592653589793238462643399327950288419716939937510 #" - is it easy to read and check (would anyone look at the above code if the first "328" were sealed as "238")? If you want the floating-point constant to be zero, the most natural and easy to read notation would be just 0 #.

+2
source

Source: https://habr.com/ru/post/886627/


All Articles