Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is using using Better? [duplicate]

Tags:

syntax

c#

Possible Duplicate:
What is the C# Using block and why should I use it?

My question: Is using using(a){do something with a} better than declaring 'a' and using it that way. ie: more secure, faster, ...

see examples for clarification.

Example 1:(without using)

StreamWriter sw;
string line;
sw = new StreamWriter("D:\\NewCon.xml");
sw.WriteLine("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>");
sw.WriteLine("<config>");
for (int i = 0; i >=36; i++)
{
    line = "";
    line = "<" + xmlnodes[i] + ">";
    line += vals[i];
    line += "</" + xmlnodes[i] + ">";
    sw.WriteLine(line);   
}
sw.WriteLine("</config>");
sw.Close();
sw.Dispose();    

Example 2:(with using)

string line;    
using (sw = new StreamWriter("D:\\NewCon.xml"))
{
    sw.WriteLine("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>");
    sw.WriteLine("<config>");
    for (int i = 0; i >= 36; i++)
    {
        line = "";
        line = "<" + xmlnodes[i] + ">";
        line += vals[i];
        line += "</" + xmlnodes[i] + ">";
        sw.WriteLine(line);
    }
    sw.WriteLine("</config>");
}
like image 793
Josef Van Zyl Avatar asked Jan 06 '11 14:01

Josef Van Zyl


4 Answers

using(a){do something with a}

This means that when the code block is done with a, the program will call a.Dispose. You know that even in the event of an exception, a will have Dispose called. It is essentially doing:

var a = IDisposable_Something;
try{.....}finally{a.Dispose();}

Is it more secure...not really, but that's not the point. The point of it is to make sure that resources which need to be cleaned up are done as soon as the program is finished with them.

So the problem with your first example, is that if somewhere along the line an exception is thrown, it won't make it to the Dispose() method. The second will. You always want to ensure Dispose gets called if it is there, because there is the possibility that the IDisposable classs won't have been written correctly, and it won't have the code to make sure that the unmanaged resources are cleaned up even if Dispose() isn't called (this is generally done in a finalizer). LInk to Dispose Pattern.

The only time I have ever seen where implementing using can be tricky is with the WCF service proxy (and you can work around that issue). There is a bug where if the proxy throws an exception at times, it will cause another exception in the Dispose() method.

http://blogs.msdn.com/b/jjameson/archive/2010/03/18/avoiding-problems-with-the-using-statement-and-wcf-service-proxies.aspx

Other than that you should generally try and put an IDisposable object in a using statement.

like image 137
kemiller2002 Avatar answered Oct 20 '22 17:10

kemiller2002


If an exception is thrown in your first example then the resource won't be disposed.

Using using ensures that the resource is disposed even when an exception is thrown.

like image 43
LukeH Avatar answered Oct 20 '22 19:10

LukeH


Example 1 should never be done. If an exception is thrown, your Dispose() call will never happen, which could be bad. using guarantees execution of Dispose(). The only good alternative to Example 1 (besides using) would be:

var iDisposableObject = new YourDisposableConstructor();
try
{
    // some code
}
finally
{
    iDisposableObject.Dispose();
}
like image 32
Dan Herbert Avatar answered Oct 20 '22 19:10

Dan Herbert


First of all, the code in example 1 should be wrapped in a try/finally block in order to be functionally equivalent with the code in example 2.

That said, I've always liked 'using' blocks for code like this. I think it generally leads to more readable code. As far as security or performance, you're not going to see much difference between example 1 and example 2, providing you use a try/finally block in the first example!

like image 34
Jesse Taber Avatar answered Oct 20 '22 18:10

Jesse Taber