Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disposing by setting to null?

I'm aware of technique to handle IDisposable in a traditional manner. Say, in OnStop() method of windows service I close message queue client:

        if (client != null)
        {
            client.Dispose();
        }

For the first time today I saw one guy doing that this way:

        using (client)
        {
            client = null;
        }

What is exactly happening inside his "using" or does he dispose correctly at all?

like image 960
Vitali D. Avatar asked Oct 18 '11 09:10

Vitali D.


1 Answers

The using(){} statement grabs a copy of the reference var so this assignment with null is ineffective.

like image 83
Henk Holterman Avatar answered Sep 20 '22 04:09

Henk Holterman