Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Constructors in C#
A constructor in C# is a special method that gets invoked automatically when an object is created. The constructor has the same name as the class and is used to initialize the object's state. Constructors do not have a return type, not even void.
Syntax
Following is the basic syntax for declaring a constructor −
public class ClassName {
public ClassName() {
// constructor body
}
}
Types of Constructors
C# supports several types of constructors −
-
Default Constructor − Takes no parameters
-
Parameterized Constructor − Takes one or more parameters
-
Copy Constructor − Creates a copy of an existing object
-
Static Constructor − Initializes static members of the class
Using Default Constructor
Example
using System;
public class Department {
public Department() {
Console.WriteLine("Constructor Invoked");
}
public static void Main(string[] args) {
Department dept1 = new Department();
Department dept2 = new Department();
}
}
The output of the above code is −
Constructor Invoked Constructor Invoked
Using Parameterized Constructor
Example
using System;
public class Student {
private string name;
private int age;
public Student(string name, int age) {
this.name = name;
this.age = age;
Console.WriteLine("Parameterized constructor called");
}
public void DisplayInfo() {
Console.WriteLine("Name: " + name + ", Age: " + age);
}
public static void Main(string[] args) {
Student s1 = new Student("Alice", 20);
Student s2 = new Student("Bob", 22);
s1.DisplayInfo();
s2.DisplayInfo();
}
}
The output of the above code is −
Parameterized constructor called Parameterized constructor called Name: Alice, Age: 20 Name: Bob, Age: 22
Using Static Constructor
Example
using System;
public class Counter {
public static int count;
static Counter() {
count = 0;
Console.WriteLine("Static constructor called");
}
public Counter() {
count++;
Console.WriteLine("Instance constructor called, count: " + count);
}
public static void Main(string[] args) {
Console.WriteLine("Creating first object:");
Counter c1 = new Counter();
Console.WriteLine("Creating second object:");
Counter c2 = new Counter();
}
}
The output of the above code is −
Static constructor called Creating first object: Instance constructor called, count: 1 Creating second object: Instance constructor called, count: 2
Constructor Overloading
C# allows multiple constructors in the same class with different parameter lists −
Example
using System;
public class Rectangle {
private int length, width;
public Rectangle() {
length = 1;
width = 1;
Console.WriteLine("Default constructor: 1x1 rectangle");
}
public Rectangle(int side) {
length = side;
width = side;
Console.WriteLine("Square constructor: " + side + "x" + side + " square");
}
public Rectangle(int l, int w) {
length = l;
width = w;
Console.WriteLine("Rectangle constructor: " + l + "x" + w + " rectangle");
}
public int Area() {
return length * width;
}
public static void Main(string[] args) {
Rectangle r1 = new Rectangle();
Rectangle r2 = new Rectangle(5);
Rectangle r3 = new Rectangle(4, 6);
Console.WriteLine("Areas: " + r1.Area() + ", " + r2.Area() + ", " + r3.Area());
}
}
The output of the above code is −
Default constructor: 1x1 rectangle Square constructor: 5x5 square Rectangle constructor: 4x6 rectangle Areas: 1, 25, 24
Conclusion
Constructors in C# are special methods that initialize objects automatically when they are created. They enable you to set initial values, validate data, and perform setup operations. C# supports default, parameterized, copy, and static constructors, allowing flexible object initialization patterns.
