Why is 'using' improving C# performances

Posted by Wernight on Stack Overflow See other posts from Stack Overflow or by Wernight
Published on 2010-06-17T14:32:58Z Indexed on 2010/06/17 14:43 UTC
Read the original article Hit count: 210

Filed under:
|
|
|
|

It seems that in most cases the C# compiler could call Dispose() automatically. Like most cases of the using pattern look like:

public void SomeMethod()
{
    ...

    using (var foo = new Foo())
    {
        ...
    }

    // Foo isn't use after here (obviously).
    ...
}

Since foo isn't used (that's a very simple detection) and since its not provided as argument to another method (that's a supposition that applies to many use cases and can be extended), the compiler could automatically call Dispose() without the developper requiring to do it.

This means that in most cases the using is pretty useless if the compiler does some smart job. IDisposable seem low level enough to me to be taken in account by a compiler.

Now why isn't this done? Wouldn't that improve the performances (if the developpers are... dirty).

© Stack Overflow or respective owner

Related posts about c#

Related posts about compiler