Question

I'm studying for the 70-536 exam and now I'm checking the lesson about converting between types and I have doubts.

Always implicit conversion it's a widening conversion? and Explicit conversion it's a narrowing conversion?

Also this is consider an Narrowing conversion?

System.Int32 number32 = 25548612
System.Int16 number16 = (System.Int16) number32;     
Was it helpful?

Solution

That narrowing conversions should be explicit, and widening conversions may be implicitly is only a design guideline. It is possible to create conversions that violate this guideline with user defined conversions. It's also possible to use an explicit conversion whenever the types implemented the implicit conversion.

  • A widening conversion is a conversion where every value of the original type can be represented in the result type.
  • A narrowing conversion is a conversion where some values of the original type cannot be represented in the result type.

Since some values of Int32 cannot be represented as Int16, this is a narrowing conversion. Depending on the compiler options, values outside the range of Int16 either overflow, or throw an exception.


Contrary to what I previously said, this concept also applies to conversions between base- and derived classes.

You need to thing of types as sets of possible values. And not about which members they have.

Every instance of the derived class is always a valid value of a variable of the base class. So casting from the derived class to the base class is widening, and thus implicit.

Some instances of the base class are not valid values of the derived class(For example they derive from a different subtree, or are instances of the base class itself). So casting from the base class to the derived class is narrowing, and thus explicit.


There are some implicit conversions, that are only widening in a loose sense, where the conversion is lossy.

In particular int/Int32 to float/Single and long/Int64 to double/Double. With these conversions some input values can only be represented approximately in the result type.

You need to look at types as a set of allowed values. Then you see that every instance of the derived class, is also an allowed value for the base class. Thus the conversion from derived to base class is widening.

And conversely there are values of the base class, that are not legal values of the derived class.

OTHER TIPS

You can turn it around:

  • a narrowing conversion will always be explicit
  • a widening conversion will be implicit.

Assuming a sane implementation of course.

What might help you more: an implicit conversion should always be 'safe' in the sense that it will not throw an exception. An explicit exception might protest.

You can rely on this for the built-in conversions. For custom conversions these are only guidelines, they can be broken.

Always implicit conversion it's widening conversion???

No. Keep in mind that you can define your own implicit conversions. You can make them widening or not, if you want.

Explicit conversion it's a narrowing conversion??

No, same reasoning.

Also this is consider an Narrowing conversion?

Yes. There's clearly a loss of information.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top