2

While working on my project, I was wondering, will using auto-destruct data if return will be inside of a using.

Example block of code:

using(ManagedObject obj = new ManagedObject()) 
{
    int usualNumber = 0;

    // some magic stuff
    ...
    // magic stuff ends

    return usualNumber; // return goes inside of 'using' brackets
}

And here is the question, will our ManagedObject which implements IDisposable be disposed by 'using' statement?

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
  • The *only* thing the [using statement](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/using-statement) does is call `Dispose` on the object being "used": and, it will do this in *all* cases (except abrupt program termination): leaving normally, exception thrown, return from inside... Also see https://stackoverflow.com/questions/75401/what-are-the-uses-of-using-in-c-sharp which shows how it could be implemented with `try..finally`. – user2864740 Jul 14 '18 at 18:18
  • [using Statement(C# Reference)](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/using-statement) – Ňɏssa Pøngjǣrdenlarp Jul 14 '18 at 18:19
  • 1
    @Plutonix That is the wrong concept. The using *statement* supports the Disposable pattern, the using *directive* imports type names into scope. – user2864740 Jul 14 '18 at 18:20
  • Fixed, clicked the wrong one – Ňɏssa Pøngjǣrdenlarp Jul 14 '18 at 18:24
  • It doesn't "destroy" your data, it simply calls the object's.Dispose method. As others have mentioned, it just surrounds the block of code in a try/finally and calls Dispose in the finally – Flydog57 Jul 14 '18 at 18:57

4 Answers4

1

using statement can be seen as try and finally combination.

Your code is equivalent to:

ManagedObject obj = new ManagedObject();

try
{
  int usualNumber = 0;

  // some magic stuff
  ...
  // magic stuff ends

  return usualNumber;
}
finally
{
  if (obj != null)
    ((IDisposable)obj ).Dispose();
}

I assume, that the answer to your question can be seen thanks to this code sample.

MacakM
  • 1,804
  • 3
  • 23
  • 46
0

using is a Syntactic sugar, it need to contain an object which implements IDisposable interface when you leave the using scope .net will call the IDisposable.Dispose method Automatically.

Here is a sample c# online

when the program leave Test method. .net will call IDisposable.Dispose method Automatically

D-Shih
  • 44,943
  • 6
  • 31
  • 51
0

Yes, It will call the Dispose() function implemented by the IDisposable interface

class Program
{
    static void Main(string[] args)
    {
        new Test().TestIt();
        Console.Read();
    }
}

class Test
{
    public int TestIt()
    {
        using (ManagedObject obj = new ManagedObject())
        { 
            int usualNumber = 0;       
            return usualNumber;
        }
    }
}

internal class ManagedObject : IDisposable
{
    public void Dispose()
    {
        Console.WriteLine("Disposed");
    }
}
L_J
  • 2,351
  • 10
  • 23
  • 28
0

Yes, even if you return from inside the using block, it will still call Dispose on obj.

Note that returning the usualNumber is perfectly fine, but if you were to return obj, that could be a problem because you would be returning a "disposed" object.

(Technically, objects could be usable after calling Dispose. For example, you could implement a Dispose that does nothing. But generally, most Dispose implementations would put the object in an unusable state).

Eren Ersönmez
  • 38,383
  • 7
  • 71
  • 92