问题
You can implicitly convert an int to a double: double x = 5;
You can explicitly convert an int to a double: double x = (double) 5;
You can explicitly convert a double to an int: int x = (int) 5.0;
Why can't you implicitly convert a double to an int?: int x = 5.0;
回答1:
The range of double
is wider than int
. That's why you need explicit cast. Because of the same reason you can't implicitly cast from long
to int
:
long l = 234;
int x = l; // error
回答2:
Implicit casting is only available when you're guaranteed that a loss of data will not occur as a result of a conversion. So you could cast an integer
to a double
, or an integer
to a long
data type. Casting from a double
to an integer
however, runs the risk of you losing data.
Console.WriteLine ((int)5.5);
// Output > 5
This is why Microsoft didn't write an implicit cast for this specific conversion. The .NET framework instead forces you to use an explicit cast to ensure that you're making an informed decision by explicitly casting from one type to another.
The implicit keyword is used to declare an implicit user-defined type conversion operator. Use it to enable implicit conversions between a user-defined type and another type, if the conversion is guaranteed not to cause a loss of data.
Source > MSDN
回答3:
C# follows the lead of Java's type system, which ranks types int
->long
->float
->double
, and specifies that any type which appears to the left of another may be cast to it. I personally think ranking types in this fashion was a bad idea, since it means that code like long l = getSomeValue(); float f=l; double d=f;
will compile cleanly without a squawk despite a severe loss of precision compared with storing l
directly to d
; it has some other unfortunate consequences as well.
I suspect Gosling ranked the types as he did in order to ensure that passing a float
and a double
to a method which is overloaded for (float,float)
and (double,double)
would use the latter. Unfortunately, his ranking has the unfortunate side-effect that passing an int
or long
to a method which is only overloaded for float
and double
will cause the method to use the float
overload rather than the double
overload.
C# and .NET follows Java's lead in preferring a long
-to-float
conversion over a long
-to-double
; the one thing which "saves" them from some of the consequent method-overloading horrors is that nearly all the .NET Framework methods with overloads for float
and double
also have an overload for Decimal
, and no implicit conversions exist between Decimal
and the other floating-point types. As a consequence, attempting to pass a long
to a method which has overloads for float
, double
, and Decimal
but not long
will result in a compilation error rather than a conversion to float
.
Incidentally, even if the people choosing which implicit conversions to allow had given the issue more thought, it's unlikely that implicit conversions from floating-point types (e.g. double
) to discrete types (e.g. int
) would have been permitted. Among other things, the best integer representation for the result of a computation that yielded 2.9999999999994 would in some cases be 2, and in other cases it would be 3. In cases where a conversion might sometimes need to be done one way and sometimes another, it's generally good for the language to require that the programmer indicate the actual intention.
回答4:
Imagine that you have this piece of code:
double x = pi/6;
double y = sin(x);
y would then be 0.5. Now suppose that x is cast as an integer as follows:
int x = pi/6;
double y = sin(x);
In this case x would be truncated to 0, and then the sin(0) would be taken, i.e. y = 0.
This is the primary reason why implicit conversion from double to int is not implemented in a lot of strong-typed languages.
The conversion of int to double actually increases the amount of information in the type, and can thus always be done safely.
回答5:
WHEN YOU CONVERT FROM DOUBLE TO INT IMPLICITLY then you are trying to store a no. with large memory into a variable having small memory(downcasting)
double d=4.5;
int x=d;//error or warning
which can be dangerous as you may lose out the information like you may lose the fractional part of a double while storing it in an integer
whereas that is not the case while storing an int value in a double variable(upcasting).
int x=2;
double d=x; //correct
so the compiler doesn't allows to implicitly convert from double to int( or storing double value in an int) because someone might do it unknowingly and expect no loss of data. But if you explicitly cast it means that you say to compiler that cast whatever be the danger ,no matter ,i will manage....hope it helps..
来源:https://stackoverflow.com/questions/26209405/why-cant-i-implicitly-convert-a-double-to-an-int