Skip to main content
C# Fundamentals for Beginners to Advanced
CHAPTER 14 Beginner

Constructors and Destructors in C#

Updated: May 17, 2026
5 min read

# CHAPTER 14

Constructors and Destructors

1. Introduction

When you buy a new phone, it doesn't arrive blank. It comes out of the box pre-configured with a default OS, a set battery level, and factory settings. In C#, we use Constructors to set up our objects the exact moment they are created, ensuring they are instantly ready for use.

2. Learning Objectives

By the end of this chapter, you will be able to:
  • Define a Constructor.
  • Create Parameterized Constructors.
  • Overload Constructors.
  • Understand the this keyword.
  • Understand Destructors and Garbage Collection.

3. What is a Constructor?

A constructor is a special method inside a class that is automatically called when an object is instantiated using the new keyword.
  • It MUST have the exact same name as the class.
  • It has no return type (not even void).
csharp
1234567891011121314151617181920
class Player
{
    public int health;

    // Default Constructor
    public Player()
    {
        health = 100; // Every player starts with 100 health automatically!
        Console.WriteLine("Player created!");
    }
}

class Program
{
    static void Main()
    {
        Player p1 = new Player(); // "Player created!" is printed instantly.
        Console.WriteLine(p1.health); // Prints 100
    }
}

4. Parameterized Constructors

You can pass data into a constructor to customize the object the moment it is built.
csharp
123456789101112131415161718192021
class Car
{
    public string model;
    public int year;

    // Parameterized Constructor
    public Car(string m, int y)
    {
        model = m;
        year = y;
    }
}

class Program
{
    static void Main()
    {
        // We MUST provide the arguments now
        Car myCar = new Car("Mustang", 2024); 
    }
}

5. The this Keyword

If your parameter names are exactly the same as your class field names, the compiler gets confused. You use the this keyword to specify: "I mean the variable belonging to *THIS* specific object."
csharp
1234567891011
class User
{
    public string name;

    public User(string name)
    {
        // name = name; // Confusing! Which is the parameter, which is the field?
        
        this.name = name; // Correct! "This object's name = the parameter name"
    }
}

6. Constructor Overloading

Just like regular methods, you can have multiple constructors in a class, as long as they have different parameters.
csharp
12345678910111213141516171819
class Product
{
    public string Name;
    public decimal Price;

    // Constructor 1: Free product
    public Product(string name)
    {
        this.Name = name;
        this.Price = 0.0m;
    }

    // Constructor 2: Priced product
    public Product(string name, decimal price)
    {
        this.Name = name;
        this.Price = price;
    }
}

7. Destructors (Finalizers)

A destructor is a special method called automatically right before the Garbage Collector destroys the object and frees its memory. It is denoted by a tilde (~).

Note: In modern C#, you rarely need to write destructors because the Garbage Collector handles memory perfectly. You only use them if you are dealing with unmanaged resources (like direct file handles or network streams).

csharp
12345678910111213
class Connection
{
    public Connection()
    {
        Console.WriteLine("Connection Opened");
    }

    // Destructor
    ~Connection()
    {
        Console.WriteLine("Connection Closed by Garbage Collector");
    }
}

8. OOP and Memory Explanation

When new Product("Apple") is called, the CLR allocates memory on the Heap. *Before* it returns the memory address to the Stack variable, it executes the constructor code. This ensures that it is literally impossible to use an object before its initial setup is fully complete.

9. Common Mistakes

  • Putting a return type on a constructor: If you write public void Player(), the compiler thinks it is a normal method, NOT a constructor. It will not be called automatically.
  • Relying on Destructors for timing: You cannot predict exactly *when* the Garbage Collector will run. Therefore, you don't know exactly when the destructor will execute. For immediate cleanup, C# uses the IDisposable interface instead.

10. Best Practices

  • Use constructors to force the caller to provide required data. If a Bank account absolutely requires an ID, make the constructor require an ID, making it impossible to create an invalid blank account.

11. Exercises

  1. 1. Create a Book class with a constructor that requires a title and pages. Use the this keyword.
  1. 2. Add a second overloaded constructor that only takes title and defaults the pages to 100.

12. MCQs with Answers

Question 1

What is the primary purpose of a Constructor?

Question 2

What is the rule for naming a Constructor?

Question 3

What return type does a constructor have?

Question 4

What is a Default Constructor?

# method Answer: b) A constructor with no parameters
Question 5

What does the this keyword refer to?

Q6. Can a class have more than one constructor? a) Yes, through constructor overloading b) No, it can only have one Answer: a) Yes, through constructor overloading
Question 7

When is a Destructor called?

Question 8

What symbol is used to denote a Destructor?

# Answer: b) ~

Q9. Is it necessary to manually write a destructor to free normal memory in C#? a) Yes b) No, the Garbage Collector handles it automatically Answer: b) No, the Garbage Collector handles it automatically

Question 10

If you do not write ANY constructor for your class, what does the compiler do?

13. Interview Questions

  • Q: Explain the purpose of the this keyword.
  • Q: What is the difference between a Constructor and a Destructor?
  • Q: Why might you make a constructor private? (Answer: To prevent external classes from creating objects, commonly used in the Singleton design pattern).

14. Summary

Constructors ensure your objects are born in a valid, ready-to-use state. You can overload them to provide multiple ways to create an object, and use the this keyword to resolve naming conflicts. Destructors exist for cleanup, but in modern C#, the Garbage Collector usually handles that for you.

15. Next Chapter Recommendation

Now that we can build solid objects, we want to start reusing code. In Chapter 15: Inheritance in C#, we will learn how to create Parent and Child classes to share logic across your application.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·