Does the using statement use only the first variable it creates?

Let's say I have a one-time MyDisposable object that takes another one-time object as a constructor parameter.

 using(MyDisposable myDisposable= new MyDisposable(new AnotherDisposable())) { //whatever } 

Assuming MyDisposable not removing AnotherDisposable inside it, the dispose method.

Is this the correct MyDisposable ? or did he also post AnotherDisposable ?

+6
source share
6 answers

using is equivalent

 MyDisposable myDisposable = new MyDisposable(new AnotherDisposable()); try { //whatever } finally { if (myDisposable != null) myDisposable.Dispose(); } 

Thus, if myDisposable does not call Dispose in AnotherDisposable , using will not call it.

+9
source

Why not invest them?

 using(var outer = new AnotherDisposable()) { using(var inner = new MyDisposable(outer)) { //whatever } } 

Now, at least you can be sure that they will be deleted correctly.

+5
source

He doesnโ€™t โ€œorderโ€ anything. It calls the Dispose method of the object used in it. His job is cleaning everything else. Perhaps calling to dispose of another object.

+1
source

In this case, he will not have AnotherDisposable . There are two solutions for this.

First, you usually do the following:

 using (AnotherDisposable anotherDisposable = new AnotherDisposable()) using (MyDisposable myDisposable= new MyDisposable(anotherDisposable)) { } 

However, there is another way. Itโ€™s normal that when a class takes a one-time, it will take care of the disposal of the object that it took. For instance. StreamReader that wraps the Stream will remove the Stream that it wraps. This means that your design will work. You can implement the same function in MyDisposable , and then the approach you MyDisposable will be fine.

0
source

The C # s Usage Operator provides a syntax shortcut for calling Dispose on objects that implement IDisposable using a try / finally block. For instance:

 using (FileStream fs = new FileStream ("myFile.txt", FileMode.Open)) { // ... Write to the file ... } 

The compiler converts this value to: FileStream fs = new FileStream ("myFile.txt", FileMode.Open);

 try { // ... Write to the file ... } finally { if (fs != null) ((IDisposable)fs).Dispose(); } 

The finally block ensures that the Dispose method is called even in case of exception 1, or that the code exits the block earlier.

Thus, to use one block, only the removal of one disposable object will be ensured. on the other hand, you can use nested operators. as

 using (myDisposable d = new myDisposable()) { using(Disposable2 d2 = new Disposable2()) { // do something and dispose... } } 

and it will be converted as

 try { // work around for myDisposable try { // work around for Disposable2 } finally { if (d2 != null) ((IDisposable)d2 ).Dispose(); } } finally { if (d!= null) ((IDisposable)d).Dispose(); } 
0
source

You only initialized one one-time variable in the using statement. AnotherDisposable nested object is created using normal initialization, not using . Thus, only the variable myDisposable that you created using the using statement will be automatically deleted by it.

0
source

Source: https://habr.com/ru/post/948130/


All Articles