Sunday, 16 April 2017

World of Constructors in C#

Constructors

     Constructor are class methods that are executed automatically when instance of class is created. Constructors are use to initialize globally declared members of given class. Constructors can only run once when instance of class is created or in other words memory is allocated for given class and even it constructor runs before any written code in class. We can have multiple constructors in class, with help of polymorphism in way of overloading. If we do not define any constructor in class then compiler will automatically create Default constructor in the class.

Notable points about Constructors
üA Class can have any numbers of constructors.
üA static constructor does not have any parameters.
üWith in class you can create only one static constructors.
üA constructor does not have any return type, not even void.

Types of Constructors
1. Default constructor - A Constructor without any parameter is called a default constructor. In this constructor every instance of class will be initialized without any parameters values. The default constructor initializes all numeric fields to zero and all strings and object to null.

2. Parametrized constructor - A Constructor with at least one parameter is called a parameterized constructor. This good side of this constructor is that you can initialize each instance with different values.
a) Constructor Overloading - We can overload constructor by creating another constructor with same method name with different parameters.

3. Copy constructor - A Parameterized constructor that contains a parameter of same class type is called copy constructor. The main purpose of copy constructor is to initialize new instance to the value of an existing instance.

4. Static constructor - When we declare constructor as static it will be invoked only once for any number of instances of class and its during creation of first instance or first reference to a static member in the class. Static constructor is used to initialize static fields of the class and to write the code that needs to be executed only once.
a) Notable points of Static constructor
i. A static constructor does not take access modifiers or have parameters.
ii. A static constructor cannot be called directly.
iii. The user has not control on when the static constructor is executed in the program because it is called by CLR during compile time.
iv. In Static constructors constants variable can be created, where values does not changes after initialization.

5. Private constructor - Private constructor is a special instance constructor used in a class that contains static member only. If a class has private constructor and no public constructor then other classes is not allowed to create instance of this class this mean we can neither create the object of the class nor it can be inherit by other class. The main purpose of creating private constructor is used to restrict the class from being instantiated when it contains every member as static.

6.  Instance constructor - A Constructor without any parameter is called instance constructor. This sounds similar to Default constructor and static constructor, but it is different.
a) Default constructor initializes default values of variable as per data types, by in instance constructor we can assign specific values to variables.
              b) In static constructor, it get called automatically from CLR, but instance constructor get called when instance of class is created.

Destructor

     Destructor are use to release memory allocated for instance of object. In .Net framework, the garbage collector automatically manages the allocation and release of memory for manages objects in your application. But there were still unmanaged code, which can be free up specifically by using Destructor.

No comments:

Post a Comment

ML.NET: Machine Learning for .NET Developers

Machine Learning in .Net ML.NET is a free software machine learning library for the C# and F# programming languages. It also supports Pyth...