Tag Archives: Puzzle

C# Puzzle: this = new Foo();

Can you imagine a situation when this code compiles and runs?

public void Reset()
{
    this = new Foo();
}

See the result: https://dotnetfiddle.net/JheuMu.

C# Puzzle: out parameters

This method usually outputs false.
Can you imagine a situation where it outputs true?

static bool Test(out int x, out int y)
{
    x = 123;
    y = 45;
    return (x == y);
}

You can find the solution in .NET Fiddle.

C# Puzzle: Exceptions

What is the output when you run this code?

try
{
  try
  {
    throw new Exception("A");
  }
  catch
  {
    throw new Exception("B");
  }
  finally
  {
    throw new Exception("C");
  }
}
catch (Exception ex)
{
  Console.Write(ex.Message);
}

See the result: https://dotnetfiddle.net/6SWPqL