212k views
0 votes
Which of the following statements is false?

1) When a decimal variable is initialized to an int value, the int value must be cast to decimal.
2) C# treats whole-number literals like 7 and 1000 as type int.
3) Unlike double values, int values can be assigned to decimal variables.
4) C# treats numeric literals like 0.05 as type double.

1 Answer

6 votes

Final answer:

The false statement is that an int value must be cast to a decimal when initializing a decimal variable. In C#, an implicit conversion exists from int to decimal, so no casting is required.

Step-by-step explanation:

The statement that is false is: "When a decimal variable is initialized to an int value, the int value must be cast to decimal." In C#, you do not need to explicitly cast an int value when assigning it to a decimal variable because there is an implicit conversion from int to decimal. However, you do need to convert from decimal to int explicitly because there is a loss of precision, and the compiler requires it to be explicit. For the other statements: C# does indeed treat whole number literals without a decimal point as int, and literals with a decimal point, like 0.05, as double. Therefore, int values can be assigned to decimal variables without casting, but not the other way around.

User Dylan Kirkby
by
8.6k points