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;Answer1:
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.<ul><li>A widening conversion is a conversion where <strong>every value</strong> of the original type <strong>can</strong> be represented in the result type.</li> <li>A narrowing conversion is a conversion where <strong>some values</strong> of the original type <strong>cannot</strong> be represented in the result type.</li> </ul>
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.<hr />
There are some implicit conversions, that are only widening in a loose sense, where the conversion is lossy.
Double. With these conversions some input values can only be represented <em>approximately</em> 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.Answer2:
You can turn it around:<ul><li>a narrowing conversion will always be explicit</li> <li>a widening conversion will be implicit. </li> </ul>
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.Answer3:
Always implicit conversion it's widening conversion???</blockquote>
No. Keep in mind that you can define your own implicit conversions. You can make them widening or not, if you want.<blockquote>
Explicit conversion it's a narrowing conversion??</blockquote>
No, same reasoning.<blockquote>
Also this is consider an Narrowing conversion?</blockquote>
Yes. There's clearly a loss of information.