Re: Do I need to call MemoryStream.Close ?

Tech-Archive recommends: Fix windows errors by optimizing your registry





Nicholas Paldino [.NET/C# MVP] wrote:

Also, it is good practice to always call Dispose on any instance of a type that implements IDisposable.

If you are the one "responsible" for that object. For example:

  void foo(Stream s) {
    s.Write(...);
    // should usually *not* invoke s.Dispose();
  }

whereas the creator of the object should (usually) either: Dispose() it when done:

  void bar() {
    using ( MemoryStream s = new MemoryStream() ) {
      foo(s);
      ...;
    }
  }

or be sure it has given that responsibility to some other code:

  class Baz: IDispoable {
    Stream s;
    void Dispose() { if ( s != null ) s.Dispose(); }
  }
  void quux(Baz baz) {
    baz.s = new MemoryStream();
  }

--
Helge Jensen
  mailto:helge.jensen@xxxxxxx
  sip:helge.jensen@xxxxxxx
               -=> Sebastian cover-music: http://ungdomshus.nu <=-
.



Relevant Pages

  • Re: Cant Invoke from System.Threading.Timer
    ... field initializer - Meaning you can't use "this" in the CheckStatusmethod ... static reference to your form (so you can call Invoke) and make SetText ... >> private void InitializeComponent() ...
    (microsoft.public.dotnet.framework.compactframework)
  • When is a form disposed? (unexpected ObjectDisposedException errors)
    ... thread and interacts with it from a worker thread. ... void MainThread() ... I have written a thread-safe Dispose ... Invoke() will fail with an ...
    (microsoft.public.dotnet.framework.windowsforms)
  • RE: 2.6.16-rc5: known regressions [TP 600X S3, vanilla DSDT]
    ... kacpid to invoke AML methods during suspend-resume period. ... static void acpi_thermal_run ... Could it cause trouble to caller? ...
    (Linux-Kernel)
  • Re: Events in C#
    ... Why would pvt_evneed a public accessor when I invoke it from the same ... are declared, the ev declaration is removed, and so invoke on evno longer ... public delegate void MyEvent; ... private event MyEvent pvt_ev; ...
    (microsoft.public.dotnet.framework)
  • Re: InvokeRequired/Invoke on CF 2.0
    ... public delegate void ObserverNotifyDelegate; ... fires from the non-UI thread, I check InvokeRequired, and if true, call ... Invoke line, but the debugger never indicates that the method is actually ...
    (microsoft.public.dotnet.framework.compactframework)