When does IDE0063 dispose?

You are using C# 8. In older C# versions that ; would have made this invalid.

In the new syntax, the client stays in scope for the surrounding method (or other {} scope block). Note that you can omit the outer pair of () as well.

It’s called a using declaration, the documentation is here.

void Method()
{
    using var client = new Client();
    // pre code...
    client.Do();
    // post code...
    // more code...
} --> client.Dispose() is called here (at the latest)

Logically the Dispose happens at the } but the optimizer might do it earlier.

Edit

I noticed that having // more code after the end of the using block, prevents this improvement from appearing. So there will be no more ambiguity if you convert the following code:

void Method()
{
    // not relevant code

    using (var client = new Client())
    {
        // pre code...
        client.Do();
        // post code...
    }
}

into this code:

void Method()
{
    // not relevant code

    using var client = new Client();
    // pre code...
    client.Do();
    // post code...
}

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)