Hello All,
Some time questions will confuse us, like this.
Answer we know, but analyzing the question and replying to that takes bit time
We easily give the answer if they ask above question like below.
What are the different types of constructor we have?
Answer for this question is two types we have. Now we will not discuss this first we will understand what is constructor then we will go to the types.
What is Constructor?
In general word its prepare item when we create the object, means it is used to keep something ready for the object when it is created.
But technically, constructor is a method of class which is invoked automatically when an object to the class is created.
Constructor name should be always same name a s class name and it does not have any return types even void.
Types of Constructors:
We have two types of constructors.
1. Instance Constructors
2. Non-Instance Constructors
Instance Constructors:
Instance constructor are constructors is used to initialize instance data. It is called every time when the object of class is created. It is called explicitly. Instance constructor takes parameters and also it has access specifiers.
Instance constructor has following types:
- Default
- Parameterized
- Copy and
- Private
User Defined Default Constructor:
- This constructor is created by the programmer.
- This constructor doesn't accept any arguments or parameters.
- In general this constructor is used to initialize required values into the fields.
- But there is no restriction to write particular code in to the constructor.
Here is the example:
public class Program { public static void Main(string[] args) { PriceCal obj=new PriceCal(); string result = obj.DisplayPrice(); Console.WriteLine(result); Console.ReadLine(); } } public class PriceCal { string CurrencyType; string Location; public PriceCal() { CurrencyType = "Rupees"; Location = "India"; } public string DisplayPrice() { return $"Hello User I am {Location}'n {CurrencyType}"; } }
Output:
Hello User I am India'n Rupees
System Defined Default Constructor:
- If there is constructor defined within class then, system will create its own constructor and will assign default values to its data filed.
- This constructor create by the system is known as system defined default constructor
- When an object of the class is created first system will search for a constructor with in the class, if there is no constructor available with in the class, system will create its own constructor and will assign default values in to the Data Fields.
Here is the example:
public class Program { public static void Main(string[] args) { PriceCal obj=new PriceCal(); string result = obj.DisplayPrice(); Console.WriteLine(result); Console.ReadLine(); } } public class PriceCal { string CurrencyType; string Location; int IncreaseCount; public string DisplayPrice() { return $"Hello User I am {Location}'n {CurrencyType} and
increase count is {IncreaseCount}"; } }
Output:
Hello User I am 'n and increase count is 0
In the above example we did not write any constructor in the class, but default values are stored in to the data fields, because is there is no constructor with in the class System Created its own constructor and assigned default value to its field.
Parameterized Constructor:
It accepts argument to store the values in data fields, with the help of this constructor we can store different set of values into different objects created to the class.
public class Program { public static void Main(string[] args) { PriceCal obj=new PriceCal("Rupees","Indian",1); string result = obj.DisplayPrice(); Console.WriteLine(result); PriceCal obj2 = new PriceCal("USD", "American", 70); string result2 = obj2.DisplayPrice(); Console.WriteLine(result2); Console.ReadLine(); } } public class PriceCal { string CurrencyType; string Location; int IncreaseCount; public PriceCal(string CurrencyType,string Location, int IncreaseCount) { this.CurrencyType = CurrencyType; this.Location = Location; this.IncreaseCount = IncreaseCount; } public string DisplayPrice() { return $"Hello User I am {Location}'n {CurrencyType} and
increase count is {IncreaseCount}"; } }
Here is the output:
Hello User I am Indian'n Rupees and increase count is 1 Hello User I am American'n USD and increase count is 70
Points to Remember:
Class can have more than one constructor and also can be overloaded for different number of arguments
Copy Constructor:
This constructor is used to copy the data of an existing object into newly created object. I we want to copy, we need to pass argument that belong to our class datatype.
We can use a copy constructor if we want to audit a class or remember the class’s old values.
The copy constructor is a parameterized constructor that accepts objects of the same type as parameters.
public class Program { public static void Main(string[] args) { PriceCal obj=new PriceCal("Rupees","Indian",1); string result = obj.DisplayPrice(); Console.WriteLine(result); PriceCal obj2 = new PriceCal(obj); string result2 = obj2.DisplayPrice_Copy(); Console.WriteLine(result2); Console.ReadLine(); } } public class PriceCal { string CurrencyType, Copy_CurrencyType; string Location,Copy_Location; int IncreaseCount, Copy_IncreaseCount; public PriceCal(string CurrencyType,string Location, int IncreaseCount) { this.CurrencyType = CurrencyType; this.Location = Location; this.IncreaseCount = IncreaseCount; } public PriceCal(PriceCal priceCal) { Copy_CurrencyType = priceCal.CurrencyType; Copy_Location = priceCal.Location; Copy_IncreaseCount = priceCal.IncreaseCount; } public string DisplayPrice() { return $"Hello User I am {Location}'n {CurrencyType}
and increase count is {IncreaseCount}"; } public string DisplayPrice_Copy() { return $"Copy Constructor Hello User I am
{Copy_Location}'n {Copy_CurrencyType} and increase count
is {Copy_IncreaseCount}"; } }
Here is the output:
Hello User I am Indian'n Rupees and increase count is 1 Copy Constructor Hello User I am Indian'n Rupees and increase count is 1
Private Constructor:
Private constructors are very important constructor, is used to ensure higher-quality code bases on complex projects.
Private constructor forces the class to provide a controlled and unified access pattern.
This C# example program uses a private constructor.
public class Program { public static void Main(string[] args) { // We can access an instance of this object that was created. // ... The private constructor was used. PriceCal test = PriceCal.Instance; // These statements show that the class is usable. Console.WriteLine(test.Pie); test.Pie++; Console.WriteLine(test.Pie); Console.ReadLine(); } } public class PriceCal { public static readonly PriceCal Instance = new PriceCal(); // Singleton pattern public double Pie; // Instance field private PriceCal() // This is the private constructor { this.Pie = 3.142; } }
Output:
3.142 4.1419999999999995
Point to remember:
A private constructor restricts access to the constructor. It ensures the object can only be created by a member in the type.
Non-Instance Constructor:
Non Instance constructor will not maintain separate instance for each data field of the class for each object of the class.
Static Constructor is part of the Non Instance Constructor.
A static constructor is used to initialize any static data, or to perform a particular action that needs to be performed only once. It is called automatically before the first instance is created or any static members are referenced.
A static members are not accessible with object instead we need to access with class name.