.NET/C# - Disposing an object with the 'using' statement

Posted by AJ Ravindiran on Stack Overflow See other posts from Stack Overflow or by AJ Ravindiran
Published on 2010-04-14T23:13:58Z Indexed on 2010/04/14 23:23 UTC
Read the original article Hit count: 385

Filed under:
|
|

Hello,

Suppose I have a method like so:

public byte[] GetThoseBytes()
{
    using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
    {
        ms.WriteByte(1);
        ms.WriteByte(2);
        return ms.ToArray();
    }
}

Would this still dispose the 'ms' object? I'm having doubts, maybe because something is returned before the statement block is finished.

Thanks, AJ.

© Stack Overflow or respective owner

Related posts about .NET

Related posts about c#