c# - "is" operator behaving a bit strangely -


1) According to my book, is operator can check that the expression E ( e type ) can be converted to target type, if E is either context conversion, boxing or unboxing. Since is in the following example does not check for three types of conversions, the code should not work, but it does:

  for a long time; // Edit - I forgot to add this line of code to my initial post int i = 100; If (I have been for a long time) / edit - in my initial post I have bet that the return is correct, but it actually gives false l = i;  

2)

a)

  b b; AA = new A (); If (A B is) B = (B) A; Int i = b .; Class A {Public Ent L = 100; } Class B: A {}  

The above code always causes time error compilation "use of assigned variable" . If the condition evaluates a isb to false , then b will not be assigned a value, but if the status is true < / Code>, then this would be and thus there would be no way to know that by allowing such a code compiler, that code will be followed by the b after the use of b Whether the details are valid or not (not because of knowing whether A is b is evaluated by true or incorrect ), but why Credit Should be? Why Intsead Can not Handle It?

b) But instead of working with non-reference types, the compiler does not complain, even if the code is the same.Why?

  int i = 100; for a long time; If (i'm taller) l = i;  

Thanks

The real difference is that int In the case, you are talking about the definite work of area ( L ). The field is definitely always (even without = 100 ) in the B case, you local Variable ( b );

This is all.


int i = 100; The local variables not definitely start as specified.

1: I do not think this return true at all / em>; It shows IDE warnings for me never to be true. Appearing in the reflector, the compiler completely removes this branch. I think the compiler relies on the basis of at least compile that it can be in the (theory) box and the exam but it already knows the answer, so it flips.

2: I still get the "assigned variable" compiler error; Due to "fixed assignment"


Comments

Popular posts from this blog

windows - Heroku throws SQLITE3 Read only exception -

lex - Building a lexical Analyzer in Java -

python - rename keys in a dictionary -