Books MVC ASP.NET C# SQL Server WCF Written Test HR Round Subscribe C# Videos C# Programs Buy DVD

C# Interview Questions on Destructors


What is a Destructor?
A Destructor has the same name as the class with a tilde character and is used to destroy an instance of a class.

Can a class have more than 1 destructor?
No, a class can have only 1 destructor.

Can structs in C# have destructors?

No, structs can have constructors but not destructors, only classes can have destructors.

Can you pass parameters to destructors?
No, you cannot pass parameters to destructors. Hence, you cannot overload destructors.

Can you explicitly call a destructor?

No, you cannot explicitly call a destructor. Destructors are invoked automatically by the garbage collector.

Why is it not a good idea to use Empty destructors?
When a class contains a destructor, an entry is created in the Finalize queue. When the destructor is called, the garbage collector is invoked to process the queue. If the destructor is empty, this just causes a needless loss of performance.

Is it possible to force garbage collector to run?

Yes, it possible to force garbage collector to run by calling the Collect() method, but this is not considered a good practice because this might create a performance over head. Usually the programmer has no control over when the garbage collector runs. The garbage collector checks for objects that are no longer being used by the application. If it considers an object eligible for destruction, it calls the destructor(if there is one) and reclaims the memory used to store the object.

Usually in .NET, the CLR takes care of memory management. Is there any need for a programmer to explicitly release memory and resources? If yes, why and how?
If the application is using expensive external resource, it is recommend to explicitly release the resource before the garbage collector runs and frees the object. We can do this by implementing the Dispose method from the IDisposable interface that performs the necessary cleanup for the object. This can considerably improve the performance of the application.

When do we generally use destructors to release resources?

If the application uses unmanaged resources such as windows, files, and network connections, we use destructors to release resources.

5 comments:

  1. I am confused with whether we should explicitly call GC to perform cleanup or not. Can anybody explain me or post a link where i can get clear idea of Garbage collection in C#. Thanks.

    ReplyDelete
  2. Yes, we can force GC to clean up the resource or memory which is still with the program by using GC.collect();

    ReplyDelete
  3. Actually, using GC.Collect() does not force garbage collection to occur at that moment. It is simply a request.

    Setting the vars to null and calling GC.Collect() can actually delay garbage collection because setting the vars to null is considered a use of the variable.

    ReplyDelete
  4. When should you use each of the following: Dispose method (inheriting IDisposable)
    Finalize (Object's method)
    Destructor

    ReplyDelete
  5. Visit http://www.industrialtrainingkolkata.com/?cat=2 for Video Tutorial

    WHAT IS A CONSTRUCTOR..??

    A constructor is a special member function whose task is to initialize the objects of it’s class. This is the first method that is run when an instance of a type is created. A constructor is invoked whenever an object of it’s associated class is created. If a class contains a constructor, then an object created by that class will be initialized automatically. We pass data to the constructor by enclosing it in the parentheses following the class name when creating an object. Constructors can never return a value, and can be overridden to provide custom intitialization functionality.
    Usually, we put the initialization code in the constructor. Writing a constructor in the class is damn simple

    For info on constructor JOIN VTS ASP.NET PROG. Call : 9830386818 for details

    ReplyDelete

If you are aware of any other C# questions asked in an interview, please post them below. If you find anything missing or wrong, please feel free to correct by submitting the form below.

 
Disclaimer - Terms of use - Contact Us