Should C# warn on null dereference?

As you probably know, the C# compiler does flow analysis on constants for the purposes of finding unreachable code. In this method the statement with the calls is known to be unreachable, and the compiler warns about it. const object x = null;void Foo(){  if (x != null)  {    Console.WriteLine(x.GetHashCode());  }} Now suppose we removed…

35

When is a cast not a cast?

I’m asked a lot of questions about conversion logic in C#, which is not that surprising. Conversions are common, and the rules are pretty complicated. Here’s some code I was asked about recently; I’ve stripped it down to its essence for clarity: class C<T> {}class D{  public static C<U> M<U>(C<bool> c)  {    return something;  }}public…

26