25 Feb 2024
Intermediate
In C#, a class constructor is used to initialize instance-specific data when objects are created, while a static constructor is used to initialize static data or perform one-time initialization tasks for the class itself.
-
Class Constructor:
- Also known as an instance constructor.
- Executes when an instance of the class is created.
- Responsible for initializing instance fields and performing any necessary setup for individual objects.
- Can be overloaded to provide multiple constructors with different parameters.
- Syntax: It does not have a return type, and its name is the same as the class name.
Example of a class constructor:
using System; public class Car { // Instance fields public string Make; public string Model; public int Year; // Class constructor public Car(string make, string model, int year) { Make = make; Model = model; Year = year; Console.WriteLine("A new car instance has been created!"); } // Method to display car details public void DisplayDetails() { Console.WriteLine($"Make: {Make}, Model: {Model}, Year: {Year}"); } } class Program { static void Main(string[] args) { // Creating an instance of Car class Car myCar = new Car("Toyota", "Camry", 2022); // Displaying car details myCar.DisplayDetails(); } }
Explanation:
- The
Car
class has a constructor that takes three parameters (make
,model
,year
) and initializes the instance fields accordingly. - In the
Main
method, an instance of theCar
class is created using the constructor, and the car details are displayed.
-
Static Constructor:
- Executes only once per type, regardless of how many instances of the type are created.
- Used to initialize static fields or perform one-time initialization tasks for the class.
- Executes before the first instance of the class is created or any static members are accessed.
- Cannot be called directly and cannot have access modifiers.
- Syntax: It does not have a return type, does not take any parameters, and its name is the same as the class name preceded by the
static
keyword.
Example of a static constructor:
using System; public class Counter { // Static field to count instances private static int instanceCount; // Static constructor static Counter() { instanceCount = 0; Console.WriteLine("Static constructor initialized."); } // Constructor to count instances public Counter() { instanceCount++; Console.WriteLine("A new instance of Counter has been created."); } // Method to display instance count public static void DisplayInstanceCount() { Console.WriteLine($"Total instances created: {instanceCount}"); } } class Program { static void Main(string[] args) { // Creating instances of Counter class Counter counter1 = new Counter(); Counter counter2 = new Counter(); // Displaying total instances Counter.DisplayInstanceCount(); } }
Explanation:
- The
Counter
class has a static fieldinstanceCount
to count the number of instances created. - The static constructor initializes
instanceCount
and displays a message when it's executed. - Each time an instance of
Counter
is created, the regular constructor incrementsinstanceCount
. - In the
Main
method, two instances ofCounter
are created, and the total instance count is displayed using the static methodDisplayInstanceCount()
.