CSharp examples for Language Basics:Number
Numeric suffixes explicitly define the type of a literal. Suffixes can be either lower- or uppercase, and are as follows:
Category | C# type | Example |
---|---|---|
F | float | float f = 1.0F; |
D | double | double d = 1D; |
M | decimal | decimal d = 1.0M; |
U | uint | uint i = 1U; |
L | long | long i = 1L; |
UL | ulong | ulong i = 1UL; |
The F and M suffixes should always be applied when specifying float or decimal literals.
Without the F suffix, the following line would not compile, because 4.5 would be inferred to be of type double, which has no implicit conversion to float:
using System;//from w w w . j a v a2s . c o m class Test { static void Main(){ float f = 4.5F; Console.WriteLine (f); } }
The same principle is true for a decimal literal:
using System;/*w ww . ja v a2 s . co m*/ class Test { static void Main(){ decimal d = -1.23M; // Will not compile without the M suffix. Console.WriteLine (d); } }