Constructor is a special type of method of a class which invokes automatically when instance of class is created. Constructor is used to object initialization and memory allocation of the class. Constructor is used to initialize private fields’ value of the class whenever instance or object of class is created. Constructor can be overloaded. When we don’t create constructor for the class, the compiler automatically create a default constructor for the class. Constructor name is always same of the class name General Syntax for Constructor [Access Modifier] ClassName([Parameters]) { } Types of Constructor
Default Constructor A constructor which has not defined any parameters or we can say without any parameters is called default constructor. It initializes the same value of every instance of class.
When to use static constructor Static constructor is very useful when you create wrapper classes for unmanaged code. You can also use it when class is using log file and constructor is used to write entries. Private Constructor Private constructor is a special constructor that generally used in class that contains only static members. If class has only private constructors not any public constructor then it is not possible to create instance of the class. Basically private constructor prevents to create instance of the class. If you want to create instance of the class which has private constructor then you need to create a public constructor along with private constructor.
You need to be a premium member to use this feature. To access it, you'll have to upgrade your membership.
Become a sharper developer and jumpstart your career.
$0
$
. 00
monthly
For Basic members:
$20
For Premium members:
$45
For Elite members: