Return an object created using USE

I create an object (obj below) when using and returning this object as part of the return.Will function, will this cause any problem similar to the object to be deleted before I try to use the return value in another function?

using (MyObject obj = new MyObject()) { . . . return obj; } 
+4
source share
9 answers

Will this be the reason that any problem like the object will be removed before I try to use the return value in another function?

Yes.

Can you explain what you are doing here? This code does not make any sense. The whole point of "use" is that you use this object only here, and then automatically get rid of your unmanaged resources, which makes it unusable. There is probably a better way to do what you want to do.

+16
source

An object will be Dispose() -d when it goes out of scope, be it return or some other code. The only purpose of using is to provide a fault-tolerant mechanism for IDisposable objects in the local area to clear everything that happens in a nested block of code.

This will lead to problems in your calling function, so do not do this.

+5
source

Your object will be deleted if you return it. It is still technically applicable since it was not garbage collected, but it will work with the Dispose function.

The rule that I follow in this case is that the method receiving the object is charged for its disposal. You do not know when this method will be completed with it, therefore it is this method that should clean up after itself when this is done.

+2
source

It’s just my personal opinion and, perhaps, not the most correct, but the construction used should be used when the unit is defined in the field of visibility, and you want the object in the construction used to be deleted.

+1
source

Yes, that will cause problems. If you have this case (I could think of a method that returns a database access class), do not use the using block, since you are not responsible for deleting this object, but the caller.

0
source

It would probably work for an object without the Dispose method, but then there would be no need to use "use".

Even if it works for your specific object, it is wrong. This is not what is used to β€œuse” for

0
source

From a technical point of view, this depends on what MyObject does in the Dispose method of the implemented IDisposable interface.

In theory, this can be perfectly normal, as you simply wrap things in an unnecessary try / catch / finally block.

 public void DoStuff() { MyObject myObject = GetMyObject(); Console.WriteLine("Name: " + myObject.Name); } private MyObject GetMyObject() { using (MyObject obj = new MyObject()) { obj.Name = "Aaron"; return obj; } } public class MyObject : IDisposable { public String Name { get; set; } #region IDisposable Members public void Dispose() { //depends what you do in here... } #endregion } 

Name: Aaron

0
source

You would get an exception from the consumer of your object, saying that the object is essentially different in that the dose of using the use area causes an implementation of IDisposable.

0
source

If you need to return a Disposable object, you need to make sure ALL code paths return the object or delete it. Note that an exception thrown from a function is valid code. To cover this case, you probably want to wrap the code segment between creating and returning in try-catch or try-finally to ensure that the object will be deleted correctly if the return statement is not successfully reached.

0
source

All Articles