# What is a Constructor?

A constructor runs when an object is created and gives it the starting values it needs; use this to tell a field from a parameter of the same name.

Apprentice · C# · OOP · Updated November 3, 2026

## After this lesson you can

- Write a constructor that gives an object its starting values

## Key takeaways

- A constructor runs once, when `new` creates the object, and gives it the starting values it needs.
- A constructor's name is exactly the class's name, and it has no return type, not even `void`.
- In `this.temperature = temperature;`, `this.temperature` is the object's field and `temperature` is the parameter.
- A field you don't set starts at its type's default, such as `0` for an `int`.
- A class can have several constructors with different parameters. A class with none gets an empty parameterless one.

Most objects need starting values to work properly. A refrigerator needs a capacity: if it starts at 0, nothing ever fits.

```csharp
Refrigerator fridge = new Refrigerator();
Console.WriteLine($"Did it fit? {fridge.AddItem(30)}");

public class Refrigerator
{
    private int capacity;
    private int usedSpace;

    public bool AddItem(int itemSize)
    {
        if (usedSpace + itemSize <= capacity)
        {
            usedSpace += itemSize;
            return true;
        }
        return false;
    }
}
```

```text
Did it fit? False
```

A field you don't set starts at its type's default, here `0`. Nothing fits in a refrigerator with no room.

## Adding a constructor

A **constructor** is a special method that runs once, when an object is created with `new`. Its only job is to set the object up. It has **no return type** (not even `void`), and its name is exactly the class's name:

```csharp
Refrigerator fridge = new Refrigerator(37, 1500);
Console.WriteLine($"Did it fit? {fridge.AddItem(30)}");

public class Refrigerator
{
    private int temperature;
    private int capacity;
    private int usedSpace;

    public Refrigerator(int temperature, int capacity)
    {
        this.temperature = temperature;
        this.capacity = capacity;
        this.usedSpace = 0;
    }

    public bool AddItem(int itemSize)
    {
        if (usedSpace + itemSize <= capacity)
        {
            usedSpace += itemSize;
            return true;
        }
        return false;
    }
}
```

```text
Did it fit? True
```

Now every refrigerator must be created with a temperature and a capacity: `new Refrigerator()` with no arguments no longer compiles. The constructor makes it impossible to forget.

## this

Inside the constructor there are two variables named `temperature`: the parameter, and the object's field. The word `this` means *the object being created*, so:

- `this.temperature` is the object's field;
- `temperature` on its own is the parameter.

`this.temperature = temperature;` copies the value passed in into the object. You can use `this.` anywhere in a class to make clear you mean the object's own member.

## More than one constructor

A class can have several constructors, as long as their parameters differ. Callers pick the one that fits:

```csharp
Refrigerator standard = new Refrigerator();
Refrigerator miniFridge = new Refrigerator(40, 300);

Console.WriteLine(standard.Describe());
Console.WriteLine(miniFridge.Describe());

public class Refrigerator
{
    private int temperature;
    private int capacity;

    public Refrigerator()
    {
        temperature = 37;
        capacity = 1500;
    }

    public Refrigerator(int temperature, int capacity)
    {
        this.temperature = temperature;
        this.capacity = capacity;
    }

    public string Describe()
    {
        return $"{capacity} units at {temperature} degrees";
    }
}
```

```text
1500 units at 37 degrees
300 units at 40 degrees
```

A constructor with no parameters is a **parameterless** (or default) constructor. If a class has no constructor at all, C# gives it an empty parameterless one, which is why `new Refrigerator()` worked at the start of this lesson.

## Check yourself

2 questions to try on the lesson page.

## So what

A constructor guarantees that every object starts life ready to work: the values it needs are given at the moment it's made, and the compiler won't let anyone skip them.

## Key terms

- [Constructor](https://www.ask-codey.com/glossary/constructor-c-sharp)
- [this](https://www.ask-codey.com/glossary/this-c-sharp)
- [Default Constructor](https://www.ask-codey.com/glossary/default-constructor-c-sharp)

## Cheat sheets

- [What is a Constructor?](https://www.ask-codey.com/cheat-sheets/working-with-objects-in-csharp-what-is-a-constructor-cheat-sheet)

Source: https://www.ask-codey.com/lessons/working-with-objects-in-csharp-what-is-a-constructor
