88752

Converting between types

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;

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.

<hr />

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.

In particular int/Int32 to float/Single and long/Int64 to double/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:

<blockquote>

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.

Recommend

  • Unable to enable scroll in tablelayoutpanel in C#
  • getJSON fails, JSON validates
  • How can I load a web page into a new window and inject JavaScript code into it?
  • Admob add within multiple activities
  • What is the difference and issues between these two clojure functions?
  • One Definition Rule: Can corresponding entities have different names?
  • Int to char conversion rule in C when int is outside the range of char
  • Casting double to integer when is it undefined behaviour
  • Why is it ambiguous to call overloaded ambig(long) and ambig(unsigned long) with an integer literal?
  • In matplotlib, how do you change the fontsize of a single figure?
  • Who propagate bugfixes across branches (corporate development)?
  • xcode don't localize specific strings
  • jQuery ready not fired after rails link_to is clicked
  • Yii2: Config params vs. const/define
  • Algorithm for a smudge tool?
  • ilmerge with a PFX file
  • script to move all files from one location to another location
  • WinForms: two way TextBox problem
  • Can I make an Android app that runs a web view in Chrome 39?
  • Convert array of 8 bytes to signed long in C++
  • Numpy divide by zero. Why?
  • php design question - will a Helper help here?
  • Comma separated Values
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • AngularJs get employee from factory
  • Error creating VM instance in Google Compute Engine
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • Hits per day in Google Big Query
  • Understanding cpu registers
  • how does django model after text[] in postgresql [duplicate]
  • How do I configure my settings file to work with unit tests?
  • Turn off referential integrity in Derby? is it possible?
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • Authorize attributes not working in MVC 4
  • Busy indicator not showing up in wpf window [duplicate]
  • Recursive/Hierarchical Query Using Postgres
  • Running Map reduces the dimensions of the matrices
  • Binding checkboxes to object values in AngularJs
  • Python/Django TangoWithDjango Models and Databases
  • Net Present Value in Excel for Grouped Recurring CF