问题
I am creating an object(obj below) in using and return that object as part of the function return.Will this cause any problem like object will be disposed before I try to use returned value in another function ?
using (MyObject obj = new MyObject())
{
.
.
.
return obj;
}
回答1:
Will this cause any problem like object will be disposed before I try to use returned value in another function?
Yes.
Can you explain what you're trying to do here? This code doesn't make any sense. The whole point of "using" is that you are using the object here only and then automatically getting rid of its scarce unmanaged resources, rendering it unusable. There is probably a better way to do what you want to do.
回答2:
The object will be Dispose()
-d when it goes out of scope, whether by return
or some other codepath. The sole purpose of using
is to provide a failsafe mechanism for IDisposable
objects of local scope to get cleaned up whatever happens in the enclosed block of code.
That's going to result in problems in your calling function, so don't do this.
回答3:
Your object will have disposed called on it right after you return it. It's still technically useable as it has not been garbage collected, but it will have the Dispose function ran.
The rule I follow in this instance is that the method receiving the object is charged with disposing it. You don't know when that method is going to be finished with it, so it is that method's responsibility to tidy up after itself when it is done.
回答4:
Just my personal opinion and might not be the most correct, but the using construct should be used when a unit of work is defined in a scope and you would like the object in the using construct to be disposed.
回答5:
Yep, this will cause problems. If you have this case (I could think of a method returning a database accessor class) don't use a using
block, as you aren't responseable for the disposal of this object, but the caller is.
回答6:
It would probably work for an object without a Dispose method, but then there would be no need for using 'using'.
Even if it works for your particular object, it is wrong. That is not what the 'using' construct is for
回答7:
Technically speaking it is contingent on what MyObject
does in the Dispose
method of the implemented IDisposable
interface.
In theory this could be perfectly fine as you are simply wrapping 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
回答8:
You would get an exception in the consumer of your object saying object is disposed as essentially that's what the using scope dose - calls the IDisposable implementation.
回答9:
If you need to return a Disposable object, you need to make sure that ALL code paths either return the object or dispose it. Note that an exception being thrown out of the function IS a valid code path. To cover this case, you probably want to wrap the segment of code between the creation and returning in a try-catch or try-finally to ensure that the object gets properly disposed if the return statement is not successfully reached.
来源:https://stackoverflow.com/questions/4228521/return-an-object-created-by-using