What is constructurs in C#
What is constructurs ?
A class constructurs is a special member function of a class that is executed whenever we create new objects of that class.A constructurs has exactly the same name as that of class and it does not have any return type.
Syntex of Constructurs
The syntax for defining a constructor in C# is straightforward. It resembles a method declaration but lacks a return type. Here’s the general syntax:
Type of constructor :
· Default constructor
· Parameterized Constructor
· Copy Constructor
· Static Constructor
· Private Constructor
Default constructor
No value is passed in the default constructor or even if we do not create a default constructor in the default constructor, the compiler allows us to create a constructor by default.
Parameterized Constructor
A constructor which has at least one parameters is callead parameterized constructor.using this type of constructor we can initialize each instance of the class to different values.
Copy Constructor
The constructor which creates an object by copying from another object is called a copy consutructor . the purpose of a copy consutructor is to initalize a new instance to the values of an existing instance.
In simple words, we can say copy consutructor is a consutructor which copies a data of one object into another object.
Static Constructor
A static consuteructor is used to initialize static variables of the class and to peroform a particular action only once.static consutrctor is called only once, no metter how many objects you create.statuic consutrctor is called before instance(default or parameterized ) consutrctor.
Private Constructor
When a consutructor is created with a private specifler , it is not possible for other classes to derive from this class , nethier is it possible to create an instance of this class. They are usually used in classed that contian static members only .
Leave a Reply
Want to join the discussion?Feel free to contribute!