Lesson Sunday

Let's learn another technique that will allow us to make our classes more flexible: overloaded constructors. Let's take a look at an example.

Let's modify our to do list to include a Priority property:

...
  public class Item
  {
    public string Description { get; set; }
    public int Priority { get; set; }
    ...

    public Item(string description, int priority)
    {
      Description = description;
      Priority = priority;
      _instances.Add(this);
    }
  ...
  }
...

Now each Item needs to have a description and a priority passed into it:

 Item practiceCoding = new Item("practice C#", 5);

However, what if we want to have an Item that doesn't have a priority yet? Overloaded constructors give us the flexibility to pass in different configurations of arguments. With an overloaded constructor, we could pass in both description and priority or we could just pass in description. Here's an example:

// Constructors below

public Item(string description)
{
    Description = description;
    _instances.Add(this);
}

public Item(string description, int priority)
{
    Description = description;
    Priority = priority;
    _instances.Add(this);
}

We've added a second constructor in the code above. The first constructor takes a description as a parameter while the second takes both description and priority as parameters.

Now we can do something like this:

 Item practiceCoding = new Item("practice C#", 5);
 Item mowLawn = new Item("mow lawn weekly");

However, our constructors aren't very DRY. Multiple lines of code are repeated. We can refactor our code with special syntax and the this keyword:

// Refactored constructors

public Item(string description)
{
  Description = description;
  _instances.Add(this);
}

public Item(string description, int priority)
  : this(description)
{
  Priority = priority;
}

Our first constructor looks the same while the second has new syntax. We add : this(description). : this() allows the second constructor to utilize everything that's in the first constructor, which means we don't need to repeat any code that's already in the first constructor. We have to pass in description because we are utilizing the first constructor and it expects a description parameter. Then our second, overloaded constructor will take care of the rest.

When to Use Overloaded Constructors


When we define two or more constructors in a class, constructors that differ from the original are called overloaded constructors. We can use overloaded constructors with the following use cases:

  • Order of parameters: If we want to pass in a priority first and then a description, we could add an overloaded constructor that accounts for arguments being passed into the constructor in a different order.
  • Type of parameters: If priority is sometimes a string (high, medium, and low) and sometimes an int (1 through 5), we can use an overloaded constructor to account for this different type of data.
  • Flexibility of parameters: We can choose to have a different number of parameters (as seen in the example we use above) or even no parameters at all.

Overloaded constructors give us additional flexibility in our code. C# is strict about the number of parameters that can be passed into any method, including a constructor. However, in the real world, data can be messy and overloaded constructors can help us write resilient, reusable applications.

Terminology


Overloaded constructor: When we define two or more constructors in a class.

Example


// Constructors below

public Item(string description)
{
    Description = description;
    _instances.Add(this);
}

public Item(string description, int priority)
{
    Description = description;
    Priority = priority;
    _instances.Add(this);
}

When to Use Overloaded Constructors


  • Order of parameters: If we want to pass in a priority first and then a description, we could add an overloaded constructor that accounts for arguments being passed into the constructor in a different order.
  • Type of parameters: If priority is sometimes a string (high, medium, and low) and sometimes an int (1 through 5), we can use an overloaded constructor to account for this different type of data.
  • Flexibility of parameters: We can choose to have a different number of parameters (as seen in the example we use above) or even no parameters at all.

Lesson 5 of 12
Last updated more than 3 months ago.