Search code examples
c#.netvb.netdisposeidisposable

Should I call Dispose() within a function after Return?


Should I call .Dispose() after returning an object that implements IDisposable?

myDisposableObject Gimme() {
  //Code
  return disposableResult;
  disposableResult.Dispose();
}

In other words, is the object I return a copy, or is it the object itself? Thanks :)


Solution

  • It's the object itself. Don't call Dispose here, even if you reverse the order so that it gets called.