C# Constructors


constructor is a special method that is called when an instance (object) of a class is created. It is used to initialize the object's properties and allocate resources. Constructors have the same name as the class and do not have a return type, not even void.

Key Features of Constructors

  1. Name: A constructor must have the same name as the class.
  2. No Return Type: Constructors do not have a return type.
  3. Overloading: You can have multiple constructors in a class with different parameters (constructor overloading).
  4. Default Constructor: If no constructor is defined, C# provides a default constructor that initializes fields to their default values.
  5. Parameterized Constructor: A constructor that takes parameters to allow for custom initialization of an object.
  6. Static Constructor: A special constructor that initializes static members of the class and is called only once, before any static members are accessed or any instances are created.

Types of Constructors

  1. Default Constructor
  2. Parameterized Constructor
  3. Static Constructor

Example of Each Type of Constructor

1. Default Constructor

A default constructor initializes an object with default values.

csharp
1public class Person 2{ 3 public string Name; 4 public int Age; 5 6 // Default constructor 7 public Person() 8 { 9 Name = "Unknown"; 10 Age = 0; 11 } 12} 13 14// Usage 15Person person1 = new Person(); // Name: Unknown, Age: 0

2. Parameterized Constructor

A parameterized constructor allows you to pass arguments to initialize the object.

csharp
1public class Person 2{ 3 public string Name; 4 public int Age; 5 6 // Parameterized constructor 7 public Person(string name, int age) 8 { 9 Name = name; 10 Age = age; 11 } 12} 13 14// Usage 15Person person2 = new Person("Alice", 30); // Name: Alice, Age: 30

3. Static Constructor

A static constructor initializes static members of the class and does not take parameters. It is called automatically before any static members are accessed or any instances are created.

csharp
1public class Configuration 2{ 3 public static string AppName; 4 5 // Static constructor 6 static Configuration() 7 { 8 AppName = "My Application"; 9 } 10} 11 12// Usage 13Console.WriteLine(Configuration.AppName); // Output: My Application

Constructor Overloading

You can define multiple constructors with different parameter lists in the same class. This is known as constructor overloading.

csharp
1public class Rectangle 2{ 3 public int Width; 4 public int Height; 5 6 // Default constructor 7 public Rectangle() 8 { 9 Width = 1; 10 Height = 1; 11 } 12 13 // Parameterized constructor 14 public Rectangle(int width, int height) 15 { 16 Width = width; 17 Height = height; 18 } 19} 20 21// Usage 22Rectangle rect1 = new Rectangle(); // Width: 1, Height: 1 23Rectangle rect2 = new Rectangle(5, 10); // Width: 5, Height: 10

Chaining Constructors

You can call one constructor from another constructor in the same class using the this keyword. This is known as constructor chaining.

csharp
1public class Circle 2{ 3 public double Radius; 4 5 // Default constructor 6 public Circle() : this(1) // Calls the parameterized constructor with a default radius of 1 7 { 8 } 9 10 // Parameterized constructor 11 public Circle(double radius) 12 { 13 Radius = radius; 14 } 15} 16 17// Usage 18Circle circle1 = new Circle(); // Radius: 1 19Circle circle2 = new Circle(3.5); // Radius: 3.5

Talk to us?

Post your blog