Why are local variables definitely assigned in unreachable statements?

You’re probably all familiar with the feature of C# which disallows reading from a local variable before it has been “definitely assigned”: void M(){  int x;  if (Q())    x = 123;  if (R())    Console.WriteLine(x); // illegal!} This is illegal because there is a path through the code which, if taken, results in the local variable…

24

Never Say Never, Part Two

Whether we have a “never” return type or not, we need to be able to determine when the end point of a method is unreachable for error reporting in methods that have non-void return type. The compiler is pretty clever about working that out; it can handle situations like int M(){  try  {    while(true) N(); …

25

Never Say Never, Part One

Can you find a lambda expression that can be implicitly converted to Func<T> for any possible T? . . . . . . . . . . . Hint: The same lambda is convertible to Action as well. . . . . . . . . . Func<int> function = () => { throw new…

27

Four switch oddities

The C# switch statement is a bit weird. Today, four quick takes on things you probably didn’t know about the switch statement. Case 1: You probably know that it is illegal to “fall through” from one switch section to another: switch(attitude){  case Attitude.HighAndMighty:    Console.WriteLine(“High”);    // we want to fall through, but this is an error …

39