12

How to destroy an object in finally block.

For example

 Public void fnName()
 {
    ClassName obj = new ClassName();
    try {

    }
    catch() {

    }
    finally {
        // destroy obj here
    }
 }
WeSt
  • 2,628
  • 5
  • 22
  • 37
sajbeer
  • 191
  • 2
  • 2
  • 10
  • obj = null; should be enough....... – Mo Patel Sep 10 '14 at 12:05
  • 3
    If the Class of the object implements IDisposable, you can call obj.Dispose(). – WeSt Sep 10 '14 at 12:06
  • @FarhadJabiyev obj = null is pretty useless if the variable obj is defined within a function scope (since the reference exists on the stack). Are you trying to clean up resources? – Bas Sep 10 '14 at 12:06
  • 8
    well, since it's a local variable, you don't really have to do `obj=null;`. If it's not referenced anywhere else, the garbage collector will take care of it. – vesan Sep 10 '14 at 12:07
  • 3
    As vesan says, let the garbage collector take of it. Unless your class implements `IDisposable` don't worry about it – Sayse Sep 10 '14 at 12:08
  • In order for anyone to answer your question well, you need to explain why you feel that it is important that some form of finalization occurs at a programmer specified point in time. – David Heffernan Sep 10 '14 at 12:24

2 Answers2

34

Do nothing. Your reference (obj) will go out of scope. Then the Garbage Collector will come along and destroy your object.

If there are (unmanaged) resources that need to be destroyed immediately, then implement the IDisposable interface and call Dispose in the finalize block. Or better, use the using statement.

EDIT

As suggested in the comments, when your ClassName implements IDisposable, you could either do:

ClassName obj = null;
try{
   obj = new ClassName();
   //do stuff
}
finally{
   if (obj != null) { obj.Dispose(); }
}

Or, with a using statement:

using (var obj = new ClassName())
{
     // do stuff
}
Neel
  • 11,625
  • 3
  • 43
  • 61
MarkO
  • 2,143
  • 12
  • 14
8

First of all, there is nothing called Destroy in C# language. Instead, we call Dispose.

The Garbage Collector automatically executes cleanup procedures when an object goes out of scope but for the unmanaged resources like sockets, db calls etc and you need to Dispose the object as shown below:

Public void fnName()
{
    ClassName obj=new ClassName();
    try
    {

    }
    catch()
    {

    }
    finally
    {
       obj.Dispose();
    }
}

...and implement Dispose functionality in your class as shown below:

      /// <summary>
      /// Dispose all used resources.
      /// </summary>
      public void Dispose()
      {
          this.Dispose(true);
          GC.SuppressFinalize(this);
      }

        /// <summary>
        /// Dispose all used resources.
        /// </summary>
        /// <param name="disposing">Indicates the source call to dispose.</param>
        private void Dispose(bool disposing)
        {
            if (this.disposed)
            {
                return;
            }

            if (disposing)
            {
                ////Number of instance you want to dispose
            }
        }

Another way to hinder the lifespan of an object is to write your code inside a using block as shown below:

using(var obj = new ClassName())
{
}

For more details for using check it here

GHC
  • 339
  • 4
  • 13
Neel
  • 11,625
  • 3
  • 43
  • 61
  • 1
    I'm not the one who downvoted but I have to ask this......can you justify why you need to call GC.SuppressFinalize(this);? – Mo Patel Sep 10 '14 at 12:09
  • "C#" cannot do any cleanup; the .NET Framework and the Garbage Collector does. "C#" is just a programming language. – Uwe Keim Sep 10 '14 at 12:13