Starting a new series to parallel the Little Wonders series. In this series, I will examine some of the small pitfalls that can occasionally trip up developers.Introduction: Of Casts and Conversions What happens when we try to assign from an int and a double and vice-versa?double pi = 3.14;
int theAnswer = 42;// implicit widening conversion, compiles!
double doubleAnswer = theAnswer;
// implicit narrowing conversion, compiler error!
int intPi = pi;As you can see from the comments above, a conversion from a value type where there is no potential data loss is can be done with an implicit conversion. However, when converting from one value type to another may result in a loss of data, you must make the conversion explicit so the compiler knows you accept this risk. That is why the conversion from double to int will not compile with an implicit conversion, we can make the conversion explicit by adding a cast:// explicit narrowing conversion using a cast, compiler
// succeeds, but results may have data loss:
int intPi = (int)pi;So for value types, the conversions (implicit and explicit) both convert the original value to a new value of the given type. With widening and narrowing references, however, this is not the case. Converting reference types is a bit different from converting value types. First of all when you perform a widening or narrowing you don’t really convert the instance of the object, you just convert the reference itself to the wider or narrower reference type, but both the original and new reference type both refer back to the same object. Read more: James Michael Hare
int theAnswer = 42;// implicit widening conversion, compiles!
double doubleAnswer = theAnswer;
// implicit narrowing conversion, compiler error!
int intPi = pi;As you can see from the comments above, a conversion from a value type where there is no potential data loss is can be done with an implicit conversion. However, when converting from one value type to another may result in a loss of data, you must make the conversion explicit so the compiler knows you accept this risk. That is why the conversion from double to int will not compile with an implicit conversion, we can make the conversion explicit by adding a cast:// explicit narrowing conversion using a cast, compiler
// succeeds, but results may have data loss:
int intPi = (int)pi;So for value types, the conversions (implicit and explicit) both convert the original value to a new value of the given type. With widening and narrowing references, however, this is not the case. Converting reference types is a bit different from converting value types. First of all when you perform a widening or narrowing you don’t really convert the instance of the object, you just convert the reference itself to the wider or narrower reference type, but both the original and new reference type both refer back to the same object. Read more: James Michael Hare
0 comments:
Post a Comment