Note that there are some explanatory texts on larger screens.

plurals
  1. PO"is" operator behaving a bit strangely
    primarykey
    data
    text
    <p>1) According to my book, <code>is</code> operator can check whether expression <code>E</code> (<code>E is type</code>) can be converted to the target type only if <code>E</code> is either a reference conversion, boxing or unboxing. Since in the following example <code>is</code> doesn’t check for either of the three types of conversion, the code shouldn’t work, but it does: </p> <pre><code> long l; // EDIT - I forgot to add this line of code in my initial post int i=100; if (i is long) //EDIT - in my initial post I've claimed condition returns true, but it really returns false l = i; </code></pre> <p>2) </p> <p>a) </p> <pre><code> B b; A a = new A(); if (a is B) b = (B)a; int i = b.l; class A { public int l = 100; } class B:A { } </code></pre> <p>The above code always causes compile time error <code>“Use of unassigned variable”</code>. If condition <code>a is B</code> evaluates to <code>false</code>, then <code>b</code> won’t be assigned a value, but if condition is <code>true</code>, then it will. And thus by allowing such a code compiler would have no way of knowing whether the usage of <code>b</code> in code following the <code>if</code> statement is valid or not ( due to not knowing whether <code>a is b</code> evaluates to <code>true</code> or <code>false</code>) , but why should it know that? Intsead why couldn’t runtime handle this? </p> <p>b) But if instead we’re dealing with non reference types, then compiler doesn’t complain, even though the code is identical.Why? </p> <pre><code> int i = 100; long l; if (i is long) l = i; </code></pre> <p>thank you</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload