Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is the purpose of double implying?

Tags:

c#

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?

like image 920
Alex Gordon Avatar asked Oct 05 '10 18:10

Alex Gordon


People also ask

What is the meaning of double implies?

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.

What is the meaning of 14 in double meaning?

Double of 14 means , twice of 14 or 2 times of 14 which is 28.

How do you spell Entendre?

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 ...


2 Answers

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?

like image 72
SLaks Avatar answered Oct 24 '22 06:10

SLaks


There are two important concepts to understand in this situation.

  1. Literal Values
  2. Implicit Conversion

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

like image 30
ChaosPandion Avatar answered Oct 24 '22 05:10

ChaosPandion