RE: Generic Dispose Function?

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance

From: TT (Tom Tempelaere) (_N_0SPAMtiti_____at_hotmail.comMAPS0_N_)
Date: 03/22/04


Date: Mon, 22 Mar 2004 08:01:10 -0800

Which is foolish, because in C++ the following is completely ok:

  SomeCls* sc = NULL;
  delete sc; // has no effect.

You don't ever need to test for NULL-ness when deleting in c++.

Tom.
     
     ----- Jeff wrote: -----
     
     In C++ I used to declare the following macro for deleteing objects:
     
     #define SAFEDELETE(x) if(x != NULL) { delete x; x = NULL; }
     
     Is there currently a way to do something similar to this in C#?
     
     I'm constantly writing...
     
     if(x != null)
     {
         x.Dispose();
         x = null;
     }
     
     and would love to just write something like...
     
     SAFEDISPOSE(x);
     
     Thanks for your help!