How to destroy an object in C# ?


Destroying an object manually in C# is simple but it’s not a good practice. Assign null value to the object and call the garbage collector’s Collect() method.

public class BaseClass
{
    public BaseClass()
    {
        Console.WriteLine("base class constructor");
    }  

    ~BaseClass()
    {
        Console.WriteLine("base class destructor");
    }

    public virtual void Print()
    {
        Console.WriteLine("I am Software Developer !!");
    }
}
class Program
{
    static void Main(string[] args)
    {
        BaseClass objBase = new BaseClass();
        objBase.Print();
        objBase = null;
        GC.Collect();          
        Console.ReadKey();
    }
}