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
What is a parameterized constructor in C# programs?
A parameterized constructor in C# is a constructor that accepts parameters to initialize an object with specific values at the time of creation. This allows you to set initial values for the object's fields or properties during instantiation, making object creation more flexible and efficient.
Syntax
Following is the syntax for declaring a parameterized constructor −
public ClassName(dataType parameter1, dataType parameter2) {
// initialization code
this.field1 = parameter1;
this.field2 = parameter2;
}
To create an object using a parameterized constructor −
ClassName objectName = new ClassName(value1, value2);
Using Single Parameter Constructor
The following example demonstrates a class with a parameterized constructor that accepts one parameter −
using System;
class Demo {
private int rank;
public Demo(int rank) {
this.rank = rank;
Console.WriteLine("RANK = {0}", rank);
}
public void DisplayRank() {
Console.WriteLine("Current rank: {0}", rank);
}
static void Main(string[] args) {
Demo obj = new Demo(5);
obj.DisplayRank();
}
}
The output of the above code is −
RANK = 5 Current rank: 5
Using Multiple Parameter Constructor
The following example shows a more comprehensive use of parameterized constructor with multiple parameters −
using System;
class Line {
private double length;
public Line(double len) {
Console.WriteLine("Object is being created, length = {0}", len);
length = len;
}
public void setLength(double len) {
length = len;
}
public double getLength() {
return length;
}
static void Main(string[] args) {
Line line = new Line(10.0);
Console.WriteLine("Length of line : {0}", line.getLength());
line.setLength(6.0);
Console.WriteLine("Length of line : {0}", line.getLength());
}
}
The output of the above code is −
Object is being created, length = 10 Length of line : 10 Length of line : 6
Constructor with Multiple Parameters
You can create constructors with multiple parameters to initialize several fields at once −
using System;
class Student {
private string name;
private int age;
private string course;
public Student(string name, int age, string course) {
this.name = name;
this.age = age;
this.course = course;
}
public void DisplayInfo() {
Console.WriteLine("Name: {0}, Age: {1}, Course: {2}", name, age, course);
}
static void Main(string[] args) {
Student student1 = new Student("Alice", 20, "Computer Science");
Student student2 = new Student("Bob", 22, "Mathematics");
student1.DisplayInfo();
student2.DisplayInfo();
}
}
The output of the above code is −
Name: Alice, Age: 20, Course: Computer Science Name: Bob, Age: 22, Course: Mathematics
Conclusion
Parameterized constructors in C# allow you to initialize objects with specific values during creation, eliminating the need to set values after instantiation. They make your code more efficient and ensure that objects are properly initialized from the moment they are created.
