Constructor
A constructor is a special method of the class which gets automatically invoked whenever an instance of the class is created. Like methods, a constructor also contains the collection of instructions that are executed at the time of Object creation. It is used to assign initial values to the data members of the same class.
class Software { // Constructor public Software() { } // Some Code........ } // an object is created of Software class, // So above constructor is called Software sw = new Software();
Contents
Points About Constructors
- Constructor of a class must have the same name as the class name in which it resides.
- A constructor can not be abstract, final, static and Synchronized.
- Within a class, you can create only one static constructor.
- A constructor doesn’t have any return type, not even void.
- A static constructor cannot be a parameterized constructor.
- A class can have any number of constructors.
- Access modifiers can be used in constructor declaration to control its access i.e which other class can call the constructor.
Different between Constructors and Method
Constructor | Method |
---|---|
A constructor is used to initialize an object | A method is used to expose the behavior of an object |
The constructor must not have a return type | The method has or not have a return type |
A constructor must be the same as the class name | Method name may or may not be same as the class name |
A constructor is invoked implicitly | A method is invoked explicitly |
Types of Constructor
Default Constructor
A constructor without any parameters is called a default constructor. If we do not create constructor the class will automatically call default constructor when an object is created.
public class User { public string firstName; public string lastName; public User() { firstName = "Soumya"; lastName = "Das"; } } class Program { static void Main(string[] args) { User user = new User(); Console.WriteLine("Full Name:" + user.firstName + " " + user.lastName); Console.ReadLine(); } }
Parametrized Constructor
A constructor with at least one parameter is called a parametrized constructor.
public class User { public string firstName; public string lastName; public User(string firstName, string lastName) { this.firstName = firstName; this.lastName = lastName; } } class Program { static void Main(string[] args) { User user = new User("Soumya","Das"); Console.WriteLine("Full Name:" + user.firstName + " " + user.lastName); Console.ReadLine(); } }
Copy Constructor
The constructor which creates an object by copying variables from another object is called a copy constructor.
public class User { public string firstName; public string lastName; public User() { } public User(User user) { this.firstName = user.firstName; this.lastName = user.lastName; } } class Program { static void Main(string[] args) { User user = new User(); user.firstName = "Soumya"; user.lastName = "Das"; User user1 = new User(user); Console.WriteLine("Full Name:" + user.firstName + " " + user.lastName); Console.ReadLine(); } }
Private Constructor
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 (except nested classes) cannot create instances of this class. The use of private constructor is to serve Singleton Design Pattern.
- One use of private constructor is when we have the only static member.
- It provides the implementation of Singleton Design Pattern.
- Once we provide constructor (private/public/any) the compiler will not add the no parameter public constructor to any class.
class Program { static void Main(string[] args) { SingleTon obj1 = SingleTon.getInstance; obj1.printText("obj1"); SingleTon obj2 = SingleTon.getInstance; obj2.printText("obj2"); Console.WriteLine("Main End"); Console.ReadLine(); } } class SingleTon { private static int objectCounter = 0; private static SingleTon instance; private SingleTon() { objectCounter++; Console.WriteLine("objectCounter : " + objectCounter.ToString()); } public static SingleTon getInstance { get { if(instance == null) instance = new SingleTon(); return instance; } } public void printText(string strData) { Console.WriteLine(strData); } }
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 Characteristic
- A static constructor does not take any access modifiers.
- A static constructor does not have a parameter.
- A static constructor is called automatically to initialize the class before the first instance is created or any static members are referenced.
- A static constructor cannot be called directly.
- The user has no control over when the static constructor is executed in the program.
- A typical use of static constructors is when the class is using a log file and the constructor is used to write entries to this file.
- A class can have only one static constructor.
- It can access only static members of a class.
public class User { public string firstName; public string lastName; public static string location; public User() { } static User() { location = "India"; } } class Program { static void Main(string[] args) { Console.WriteLine("Static variable:" + User.location); Console.ReadLine(); } }