2

I am reviewing some C# code and it has been a while since I have worked with C#, so I wanted to be sure my instincts are correct. In this code I see a number of places inside a using block where something like the following is done :

using(StreamWriter thing = new StreamWriter()) {

    DataSet DS = SQLQueryMethod();
    do stuff to DS and log that stuff to the a log file using thingy...

    DS.clear();
    DS.Dispose();
}

Now, I have done a little bit of research on this as well as looked back several years through my failing memory and I think there are a number of way to do this that would be better. I would like to know which is the more "standard"/"best pactice" way. I am thinking number 1 below is the best way to do this.

thanks in advance.

  1. Add the DataSet to the using statement so that it gets disposed of automatically with the end of the scope of the using statement, obviating the need for both the clear and dispose. I would do this :

    using(StreamWriter thing = new StreamWriter(), DataSet DS = new DataSet()) {
    
        DS = SQLQueryMethod()
        do stuff to DS{...}
    
    }
    
  2. Just call the dispose on DataSet DS since I think clearing it just prior to a dispose is useless.

  3. It is actually necessary to do it the original way.

braX
  • 11,506
  • 5
  • 20
  • 33
Terry H
  • 310
  • 4
  • 19
  • This question probably belongs to http://codereview.stackexchange.com but to answer your question yes, yes, yes, but I still think you need 2 seperate usings – johnny 5 Sep 23 '16 at 14:11
  • As soon as I see `.Dispose()` in a method instantiating that same object, I always cringe and see how to rewrite that into the `using` pattern which is blatantly easy almost all of the time. – Ray Sep 23 '16 at 14:13
  • I see several answers with multiple using statements instead of adding multiple items to a single using statement, is there a difference between them? Example : using (itemA, itemB) { } vs using (itemA) using(itemB) { } – Terry H Sep 23 '16 at 14:18
  • since you are inside of the Using technically you do not have to call the dispose but you could do something like this inside of the using for the DataSet if you want to dispose of it using 1 line of code within the using `((IDisposable)DS).Dispose();` – MethodMan Sep 23 '16 at 14:33
  • I refer to the Microsoft link below on the using statement which shows a single using statement with a comma separated list of items third codebloc example : https://msdn.microsoft.com/en-us/library/yh598w02.aspx OK, did not fully read it. It says multiple instances of a TYPE, so multiple statements for each different TYPE I guess. My bad. – Terry H Sep 23 '16 at 14:36

2 Answers2

6

Multiple "using statement" can be used as follow:

using (StreamWriter thing = new StreamWriter())
using (DataSet DS = new DataSet())
{
    DS = SQLQueryMethod();
    //do stuff ....
} 

Because StreamWriter and DataSet implements the required Dispose() method in IDisposable interface, no need to explicitly call it afterward

Kevin Le - Khnle
  • 10,579
  • 11
  • 54
  • 80
0

Correct way is to dispose everything that implements IDisposable - like

using (var thing = new StreamWriter())
using (var ds = SQLQueryMethod())
{
    do stuff to DS {}
} 

Other suggested answers are wrong

using(StreamWriter thing = new StreamWriter())
using(DataSet DS = new DataSet()) 
{
  // this won't compile - DS is in using so it is a read-only variable
  DS = SQLQueryMethod()
  do stuff to DS{...}
} 

Reference: Why is a variable declared in a using statement treated as readonly?

Community
  • 1
  • 1
Ondrej Svejdar
  • 21,349
  • 5
  • 54
  • 89