C# Constructor
In C#, a constructor is a special method that is automatically called when an instance of a class (an object) is created. It is used to initialize objects and set initial values for fields or properties of the class.
Key Features of Constructors:
- Same Name as the Class: The constructor has the same name as the class in which it is defined.
- No Return Type: Constructors do not have a return type, not even
void
. - Automatic Invocation: The constructor is called automatically when the
new
keyword is used to create an object. - Initialization of Fields: It is commonly used to assign initial values to fields or properties of an object.
Syntax of a Constructor in C#:
Example of a Constructor in C#:
Types of Constructors in C#:
Default Constructor:
- A default constructor is a constructor with no parameters. If no constructor is defined in a class, the C# compiler provides a default constructor that initializes fields to their default values (e.g.,
0
for numeric types,null
for reference types).
Example of a default constructor:
- A default constructor is a constructor with no parameters. If no constructor is defined in a class, the C# compiler provides a default constructor that initializes fields to their default values (e.g.,
Parameterized Constructor:
- A parameterized constructor allows you to pass arguments to set initial values for fields when the object is created.
Example of a parameterized constructor:
Static Constructor:
- A static constructor is used to initialize static fields or perform actions that need to be executed only once. It is called automatically before the first instance of the class is created or any static members are accessed.
- A static constructor cannot take parameters and is defined using the
static
keyword.
Example of a static constructor:
Private Constructor:
- A private constructor is used to restrict the instantiation of a class. It is typically used in singleton design patterns where only one instance of a class is allowed.
Example of a private constructor:
Constructor Overloading:
In C#, you can define multiple constructors within a class with different sets of parameters. This is called constructor overloading, and it allows for different ways to create objects based on the provided arguments.
Example of constructor overloading:
Constructor Chaining:
Constructor chaining refers to calling one constructor from another constructor. This helps in reducing code duplication when initializing fields.
You can use the this
keyword to chain constructors.
Example of constructor chaining:
Conclusion:
- A constructor is a special method in C# used to initialize objects.
- It has the same name as the class, no return type, and can be parameterized or non-parameterized.
- Constructors can be overloaded to offer different ways of initializing an object.
- Special types of constructors, such as static, private, and default constructors, provide specific functionalities, like initializing static fields or restricting instantiation.