English speaking and learning Interview Questions and Answers Programs C# Concept OOPS Concept JOBS

Saturday, June 3, 2017

Constructor in C#

Constructor has the same name as its class and is syntactically similar to a method. However, constructors have no explicit return type.It gets executed when object of class is created.you will use a constructor to give initial values to the instance variables defined by the class or to perform any other startup procedures required to create a fully formed object.

All classes have constructors, whether you define one or not, because C# automatically provides a default constructor that causes all member variables to be initialized to their default values. For most value types, the default value is zero. For bool, the default is false. For reference types, the default is null. However, once you define your own constructor, the default constructor is no longer used.

A constructor can only be called by: 
  • The Compiler using the New keyword to initialize a class/struct. 
  • Another constructor of the same class/struct using the this keyword. 
  • Constructors of the derived class using the base() keyword.

Types of Constructors in C# 
  1. Default constructor. 
  2. Parameterized Constructor. 
  3. Static Constructor. 
  4. Private Constructor.

Default Constructors 
A Constructor with no parameter is called a Default Constructor. A Default Constructor is called by the compiler when no arguments are passed to the New operator while creating an object of a class or struct. If there is no constructor in a non-static class then a Public Default Constructor is provided by the compiler so that a class can be instantiated. 

 Parameterized Constructors
 A constructor with parameters is called a parameterized Constructor. A class or struct can have multiple parameterized constructors as long as they have a different method signature. They follow the same concept as method overloading. The compiler provides Default Constructors only if there is no constructor (default or parameterized) defined in a class. Parameterized Constructors can exist even without the existence of Default Constructors.

Private Constructors 

A private constructor is a special instance constructor. It is generally used in classes that contain static members only. If a class has one or more private constructors and no public constructors, other classes cannot create instances of this class. The main purpose of creating private constructor is used to restrict the class from being instantiated when it contains every member as static.If we want to create object of class even if we have private constructors then we need to have public constructor along with private constructor

static constructor
static constructor, which initializes static members of the type. Static constructors are parameterless. If you don't provide a static constructor to initialize static fields, the C# compiler will supply a default static constructor that initializes fields to their default value

When we declared constructor as static it will be invoked only once for any number of instances of the class and it’s during the creation of first instance of the class or the 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.

  • Static constructor will not accept any parameters because it is automatically called by CLR. 
  • Static constructor will not have any access modifiers. 
  • Static constructor will execute automatically whenever we create first instance of class - Only one static constructor will allowed.
  • To initialize a Static Class or static variables in a non-static class, Static Constructors are used 
  • Access Modifiers are not allowed on Static Constructors 
You may also like

No comments :

Post a Comment