What is Constructor and Destructor Asp Net C#

Q1: What is a Constructor?
A special method of the class that is automatically invoked whenever an instance of that class is created.
The main use of constructors is to initialize the private data members of the class while creating an instance for the class.
If however, there is no constructor inside the class, the compiler will automatically create a default constructor of the class.
The default constructor initializes all numeric fields in the class to zero and all string and object fields to null.
— Constructor name must be the same as its class name.
— A constructor must have no explicit return type, not even void.
— A class can have any number of constructors.
 
Q2: What are different type of Constructor in Asp Net?
1. Default Constructor
A constructor without any parameters is called a default constructor.
If we do not create any constructor explicitly,  the compiler itself will automatically call default constructor when an object is created.
    The default constructor initializes all numeric fields in the class to zero and all string and object fields to null.
2. Parameterized Constructor
A constructor with at least one parameter.
The advantage of a parameterized constructor is that you can initialize each instance of the class with a different value.
3. Copy Constructor
    A member function which is used to create a copy of an already existing object
4. Static Constructor
    A static constructor is used to initialize any static data, or to perform a particular action that needs to be performed once only.
It is called automatically before the first instance is created or any static members are referenced.
— Static constructor is created using a static keyword,
— Static constructor will be invoked only once for all of the instances of the class
— Static constructor does not take access modifiers or have parameters.
— Static constructor cannot be called directly.
— A class can have only one static constructor.
— Static constructor can access only static members of a class.
— Common use are : Create Logs
5. Private Constructor
    If a constructor is created with private specifier is known as Private Constructor.
It is not possible for other classes to derive from this class and neither it is possible possible to create an instance of this class.
— Use private constructor when class have only static members.
— Using private constructor, prevents the creation of the instances of that class.
— If a class contains only private constructor without parameter, then it prevents the automatic generation of default constructor.
— If a class contains only private constructors and does not contain public constructor, then other classes are not allowed to create
       instances of that class except nested class
Q3: What is a Destructor?
A method used for freeing up the resources allocated to an object.
This method is automatically invoked once an object of the class is destroyed.
Destructor can be used to cleanup resources used by the object.
Destructor Have the same name as the class name but prefix with ~ tilde.

— Destructors can be used only in a classes and a class can contain only one destructor.
— Destructor won’t accept any parameters and access modifiers.
— Destructor will invoke automatically, whenever an instance of a class is no longer needed.
— Destructor automatically invoked by garbage collector whenever the class objects that are no longer needed in the application.

Q4: When will be the object of a class gets destroyed in C#?
garbage collector will destroy The object of a class in following cases: –
— At the end of a program execution each and every object that is associated with the program will be destroyed by the garbage
       collector.
— The Implicit calling of the garbage collector occurs sometime in the middle of the program execution provided the memory is
        full so that the garbage collector will identify unused objects of the program and destroys them.
— The Explicit calling of the garbage collector can be done in the middle of program execution with the help of “GC.Collect()”
        statement so that if there are any unused objects associated with the program will be destroyed in the middle of the program
        execution.

Leave a Reply

Your email address will not be published.