Algorithm
In C#, a constructor is a special method in a class that is called when an instance of the class is created. It is used to initialize the object's state and perform any necessary setup tasks. Constructors have the same name as the class and do not have a return type.
Types of Constructors
There are the following types of constructors:
Parameterless Constructor
Parameterized Constructor
Default Constructor
Code Examples
#1 Parameterless Constructor in C# Program
Code -
C# Programming
using System;
namespace Constructor {
class Car {
// parameterless constructor
Car() {
Console.WriteLine("Car Constructor");
}
static void Main(string[] args) {
// call constructor
new Car();
Console.ReadLine();
}
}
}
Copy The Code &
Try With Live Editor
Output
#2 Parameterized Constructor in C# Program
Code -
C# Programming
using System;
namespace Constructor {
class Car {
string brand;
int price;
// parameterized constructor
Car(string theBrand, int thePrice) {
brand = theBrand;
price = thePrice;
}
static void Main(string[] args) {
// call parameterized constructor
Car car1 = new Car("Bugatti", 50000);
Console.WriteLine("Brand: " + car1.brand);
Console.WriteLine("Price: " + car1.price);
Console.ReadLine();
}
}
}
Copy The Code &
Try With Live Editor
Output
Price: 50000
#3 Default Constructor in C# program
Code -
C# Programming
using System;
namespace Constructor {
class Program {
int a;
static void Main(string[] args) {
// call default constructor
Program p1 = new Program();
Console.WriteLine("Default value of a: " + p1.a);
Console.ReadLine();
}
}
}
Copy The Code &
Try With Live Editor
Output
#4 Copy Constructor in C# Program
Code -
C# Programming
using System;
namespace Constructor {
class Car {
string brand;
// constructor
Car (string theBrand) {
brand = theBrand;
}
// copy constructor
Car(Car c1) {
brand = c1.brand;
}
static void Main(string[] args) {
// call constructor
Car car1 = new Car("Bugatti");
Console.WriteLine("Brand of car1: " + car1.brand);
// call the copy constructor
Car car2 = new Car(car1);
Console.WriteLine("Brand of car2: " + car2.brand);
Console.ReadLine();
}
}
}
Copy The Code &
Try With Live Editor
Output
Brand of car2: Bugatti
#5 Private Constructor in C# program
Code -
C# Programming
using System;
namespace Constructor {
class Car {
// private constructor
private Car () {
Console.WriteLine("Private Constructor");
}
}
class CarDrive {
static void Main(string[] args) {
// call private constructor
Car car1 = new Car();
Console.ReadLine();
}
}
}
Copy The Code &
Try With Live Editor
#6 Static Constructor in C# program
Code -
C# Programming
using System;
namespace Constructor {
class Car {
// static constructor
static Car () {
Console.WriteLine("Static Constructor");
}
// parameterless constructor
Car() {
Console.WriteLine("Default Constructor");
}
static void Main(string[] args) {
// call parameterless constructor
Car car1 = new Car();
// call parameterless constructor again
Car car2 = new Car();
Console.ReadLine();
}
}
}
Copy The Code &
Try With Live Editor
Output
Default Constructor
Default Constructor
#7 Constructor Overloading in C# program
Code -
C# Programming
using System;
namespace ConstructorOverload {
class Car {
// constructor with no parameter
Car() {
Console.WriteLine("Car constructor");
}
// constructor with one parameter
Car(string brand) {
Console.WriteLine("Car constructor with one parameter");
Console.WriteLine("Brand: " + brand);
}
static void Main(string[] args) {
// call constructor with no parameter
Car car = new Car();
Console.WriteLine();
// call constructor with parameter
Car car2 = new Car("Bugatti");
Console.ReadLine();
}
}
}
Copy The Code &
Try With Live Editor
Output
Car constructor with one parameter
Brand: Bugatti
Demonstration
C# Programming Constructor