Usually I use a memory stream with the well known using pattern.
using(var mem = new MemoryStream(blob))
{
foo(mem);
}
No imagine a function bar(Func<Stream>) defined in a client library that I have to use. I could call it like this
bar(() => new MemoryStream(blob));
but then nobody is disposing the stream properly. How to work around it? Should Func be used with IDisposable types at all?
FUNC? Why return a stream object at all? If you need an array of bytes or a file or something to that affect you can always pass that back from yourFUNCauX if the OP adds a using statement inside of the function, the stream will be disposed upon the call to return..Dispose()method by handIDisposableis the owner, so the one who creates an object is responsible for disposing of it. So, thebarmethod should dispose it. However, it's unclear whether theFunc<Stream>will create an object or will just provide an existing instance. In that case, the class that provides theFunc<Stream>should dispose of the object.