1

Consider the following

using (Entity e = new Entity())
{
     goto: Mars
}

Do I have a leak? Not that I'm gonna do such foolishness but just wondering.

Yuval Itzchakov
  • 146,575
  • 32
  • 257
  • 321
Zuzlx
  • 1,246
  • 14
  • 33

1 Answers1

2

Yes Dispose will be called. using translates into try/finally block something like:

try
{
    goto Mars;
    Console.WriteLine("in try");
}
finally
{

    Console.WriteLine("in finally");
}
Mars:
Console.WriteLine("in Mars");

The output from above would be:

in finally
in Mars

You can also test it by creating your own class that implements IDisposable like:

class MyDisposable : IDisposable
{
    public void Dispose()
    {
        Console.WriteLine("Dispose called");
    }
}

and then:

using (MyDisposable myDisposable = new MyDisposable())
{
    goto Mars;
}

Mars:
Console.WriteLine("in Mars");

The output would be:

Dispose called
in Mars
Habib
  • 219,104
  • 29
  • 407
  • 436