A New Approach to C # 7 Modeling and Reachability

This is based on this question, which seems to me a glitch in the language or a missing warning in the compiler. My reasoning is that is will always return true in expressions of the following type:

 int i; if (i is var j) .... 

Taking another step, consider the following code:

 int i; if (i is int) .... //Compiler warning: The given expression is always //of the provided ('int') type. 

But, as with var , follow these steps:

 if (i is int j) ... else ... 

You will not receive a warning (no expression is always true or unreachable code found in the else clause), which is essentially the same behavior.

It makes me think, maybe my initial reasoning is wrong. Is there some obscure use / angle case that I'm missing if that really matters? And if not, is the missing warning and inconsistent behavior an oversight (or even an error) in the compiler?

+6
source share
1 answer

I believe this was done on purpose, citing Roslyn source code, starting here: Binder_Operators, Line 2848

Since the heuristic presented here is used to modify the coding, it should be conservative. It is acceptable for us not to report a warning in cases where people can logically infer that the operator will always return [true]. It is unacceptable to incorrectly warn that the operator will always return [true] if there are cases when it can [fail].

+1
source

Source: https://habr.com/ru/post/1271233/


All Articles