C# Numeric Literals
In this chapter you will learn:
Integral literals
Integral literals can use decimal or hexadecimal notation; hexadecimal is denoted with the 0x prefix. For example:
int x = 127;
long y = 0x7F;
Real literals
Real literals can use decimal and/or exponential notation. For example:
double d = 1.5;
double million = 1E06;
Numeric literal type inference
By default, the compiler infers a numeric literal to be either double or an integral type:
- If the literal contains a decimal point or the exponential symbol (E), it is a double.
- Otherwise, the literal's type is the first type in this list that can fit the literal's value: int, uint, long, and ulong.
For example:
Console.WriteLine ( 1.0.GetType()); // Double (double)
Console.WriteLine ( 1E06.GetType()); // Double (double)
Console.WriteLine ( 1.GetType()); // Int32 (int)
Console.WriteLine ( 0xF0000000.GetType()); // UInt32 (uint)
//from w ww .j a v a2 s . co m
Next chapter...
What you will learn in the next chapter: