for example:
const decimal dollars = 25.50M;
why do we have to add that M
?
why not just do:
const decimal dollars = 25.50;
since it already says decimal
, doesnt it imply that 25.50
is a decimal?
The biconditional or double implication p ↔ q (read: p if and only if q) is the statement which asserts that p and q if p is true, then q is true, and if q is true then p is true. Put differently, p ↔ q asserts that p and q have the same truth value.
Double of 14 means , twice of 14 or 2 times of 14 which is 28.
The definition of double entendre—which has French origins—is “a twofold meaning” or “double meaning.” In French, “entendre” means “to hear,” “to understand,” or “to mean.” However, “entendre” is an obsolete French word, replaced with the word “entente.” English language speakers use “double entendre” today, while the ...
No.
25.50
is a standalone expression of type double
, not decimal
.
The compiler will not see that you're trying to assign it to a decimal
variable and interpret it as a decimal.
Except for lambda expressions, anonymous methods, and the conditional operator, all C# expressions have a fixed type that does not depend at all on context.
Imagine what would happen if the compiler did what you want it to, and you called Math.Max(1, 2)
.Math.Max
has overloads that take int
, double
, and decimal
. Which one would it call?
There are two important concepts to understand in this situation.
Essentially what you are asking is whether a literal value can be implicitly converted between 2 types. The compiler will actually do this for you in some cases when there would be no loss in precision. Take this for example:
long n = 1000; // Assign an Int32 literal to an Int64.
This is possible because a long
(Int64) contains a larger range of values compared to an int
(Int32). For your specific example it is possible to lose precision. Here are the drastically different ranges for decimal
and double
.
Decimal: ±1.0 × 10−28 to ±7.9 × 1028 Double: ±5.0 × 10−324 to ±1.7 × 10308
With knowledge it becomes clear why an implicit conversion is a bad idea. Here is a list of implicit conversions that the C# compiler currently supports. I highly recommend you do a bit of light reading on the subject.
Implicit Numeric Conversions Table
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With