Is it used better?

Possible duplicate:
What is a C # Using block and why should I use it?

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

see examples for explanation.

Example 1: (without use)

 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: (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>"); } 
+6
syntax c #
source share
8 answers

using (a) {do something with}

This means that when a code block is executed with a, the program will call a.Dispose. You know that even in the event of an exception, you will have Dispose. In fact:

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

Itโ€™s safer ... actually, but thatโ€™s not the point. The point is to make sure that the resources that need to be cleaned up are executed as soon as the program ends with them.

So the problem with your first example is that if an exception is thrown somewhere along the line, it will not apply to the Dispose() method. The second will be. You always want Dispose to be called if it is, because there is a chance that the IDisposable classes will not be written correctly and it will not have code to make sure that unmanaged resources are cleared even if Dispose() not called (this usually done in the finalizer). LInk to delete the template.

The only time I have ever seen that deployment can be complicated is the WCF service proxy (and you can work around this problem). There is an error in which if the proxy occasionally throws an exception, it raises 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 usually try to put the IDisposable object in the using statement.

+11
source share

If an exception is thrown in the first example, the resource will not be deleted.

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

+6
source share

Example 1 should never be executed. If an exception is thrown, your call to Dispose() will never happen, which may be bad. using guarantees the execution of Dispose() . The only good alternative to example 1 (other than using ) would be:

 var iDisposableObject = new YourDisposableConstructor(); try { // some code } finally { iDisposableObject.Dispose(); } 
+2
source share

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

However, I always liked using blocks for such code. I think this generally leads to more readable code. Regarding security or performance, you won't see much of a difference between Example 1 and Example 2, because in the first example you use the try / finally block!

+1
source share

This is safer in the sense that, as a rule, you remember to manage resources, especially in case of an exception (your code will not catch at the moment).

This is more concise and idiomatic as you use fewer lines of code and the intent of your code becomes much clearer.

So this is:

 StreamWriter sw; try { sw = new StreamWriter("D:\\epkDATA\\NewCon.xml"); ... } finally { sw.Dispose(); } 

equivalent to:

 using(StreamWriter sw = new StreamWriter("D:\\epkDATA\\NewCon.xml")) { ... } 
0
source share

Using using safer because you guaranteed the release of resources declared to be in use (...) even when an error or unexpected exit occurred.

0
source share

In addition to the previous answers and the imported part itself, if an exception occurs, a call to dispose() guaranteed.

0
source share

if ClassA inherits from IDisposing.A. Using a template is the following:

 IDisposable iDisposableObject = new YourDisposableConstructor(); try { // some code } finally { iDisposableObject.Dispose(); } 

so we better use a usage pattern

0
source share

All Articles