Reputation: 35
For example:
private const double MAX_BATTERIJ = 100D, MIN_BATTERIJ = 0D;
What is the D here? I've also seen an m but have no clue what it does.
Upvotes: 1
Views: 1615
Reputation: 250
It is the language reserved type indicator, indicating what type the literal number should be interpreted to. A way to understand this is looking at some code:
var x = 20D;
Console.WriteLine(x.GetType()); // Will write System.Double
var y = 20M;
Console.WriteLine(y.GetType()); // Will write System.Decimal
Here it is obvious that the compiler casts the literal type of the number 20 using the type indicator to the matching var type. Without the suffix it would not know which type of variable to create the "var" as. It could be an int, a decimal, a double and so on. Without you might later use is against another type and experience a type/cast exception trying to use as the wrong type.
Hope this way of seeing it helps clarify the use.
There is also a great tutorial on the subject here https://www.c-sharpcorner.com/article/data-type-suffixes-in-c-sharp/
Upvotes: 1
Reputation: 45987
Real literals The type of a real literal is determined by its suffix - reference
decimal x = 20M; //decimal
double x = 20D; //double
float x = 3.3F; //float
site note - there are also Integer literals reference
long x = 20L; //long
uint x = 2U; //Unsigned Int
ulong x = 2UL; //Unsigned Long
double x = 2.1E2; //exponent (210)
int x = 0xCA; //hex
Upvotes: 6