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
Default constructor in C#
A default constructor in C# is a constructor that takes no parameters. When you create an object of a class, the constructor is automatically invoked. The constructor has the same name as the class and is used to initialize the object.
If you don't explicitly define any constructor in your class, C# automatically provides a default constructor. However, if you define any parameterized constructor, you must explicitly define the default constructor if you want to use it.
Syntax
Following is the syntax for declaring a default constructor −
public class ClassName {
public ClassName() {
// initialization code
}
}
To create an object using the default constructor −
ClassName obj = new ClassName();
Implicit Default Constructor
When no constructor is defined, C# provides an implicit default constructor −
using System;
public class Student {
public string name;
public int age;
public void DisplayInfo() {
Console.WriteLine("Name: " + name + ", Age: " + age);
}
}
public class Program {
public static void Main(string[] args) {
Student student = new Student(); // Uses implicit default constructor
student.name = "John";
student.age = 20;
student.DisplayInfo();
}
}
The output of the above code is −
Name: John, Age: 20
Explicit Default Constructor
You can explicitly define a default constructor to perform custom initialization −
using System;
public class Department {
public string name;
public int employeeCount;
public Department() {
Console.WriteLine("Default Constructor Invoked");
name = "General";
employeeCount = 0;
}
public void DisplayInfo() {
Console.WriteLine("Department: " + name + ", Employees: " + employeeCount);
}
public static void Main(string[] args) {
Department dept1 = new Department();
dept1.DisplayInfo();
Department dept2 = new Department();
dept2.name = "IT";
dept2.employeeCount = 25;
dept2.DisplayInfo();
}
}
The output of the above code is −
Default Constructor Invoked Department: General, Employees: 0 Default Constructor Invoked Department: IT, Employees: 25
Default Constructor with Parameterized Constructor
When you define a parameterized constructor, the implicit default constructor is no longer available. You must explicitly define it if needed −
using System;
public class Employee {
public string name;
public double salary;
// Explicit default constructor
public Employee() {
name = "Unknown";
salary = 0.0;
Console.WriteLine("Default constructor called");
}
// Parameterized constructor
public Employee(string empName, double empSalary) {
name = empName;
salary = empSalary;
Console.WriteLine("Parameterized constructor called");
}
public void DisplayInfo() {
Console.WriteLine("Employee: " + name + ", Salary: $" + salary);
}
}
public class Program {
public static void Main(string[] args) {
Employee emp1 = new Employee();
emp1.DisplayInfo();
Employee emp2 = new Employee("Alice", 50000);
emp2.DisplayInfo();
}
}
The output of the above code is −
Default constructor called Employee: Unknown, Salary: $0 Parameterized constructor called Employee: Alice, Salary: $50000
Conclusion
A default constructor in C# is a parameterless constructor that initializes objects with default values. C# provides an implicit default constructor when no constructors are defined, but you must explicitly define it when parameterized constructors exist. Default constructors are essential for object initialization and are automatically called during object creation.
