Return an object created by USING

后端 未结 9 1498
南方客
南方客 2021-01-18 05:32

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 us

相关标签:
9条回答
  • 2021-01-18 05:37

    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.

    0 讨论(0)
  • 2021-01-18 05:39

    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.

    0 讨论(0)
  • 2021-01-18 05:40

    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

    0 讨论(0)
  • 2021-01-18 05:41

    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

    0 讨论(0)
  • 2021-01-18 05:45

    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.

    0 讨论(0)
  • 2021-01-18 05:56

    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.

    0 讨论(0)
提交回复
热议问题