Skip to content

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.

Default Constructors and Their Invocation in C#

C# Constructors force different behaviors on your classes and help us initialize instance fields or properties with default values.

Feb 11, 2020 • 5 Minute Read

Introduction

All things have a beginning. When a class or struct is created in C#, its constructor is called. The typical invocation of a constructor is done with the new keyword. The constructor allows the instance to be initialized with either default or custom properties based on the arguments the constructor takes. The constructor carries the same name as the class from which the instance is constructed.

The Constructor

Let's take a simple example to demonstrate constructors. We'll to define a class with two different constructors, which is called overloading.

      using System;
namespace Pluralsight
{
    public class DefaultConstructors
    {
        bool isInitialized;
        private int a;
        private int b;
        public DefaultConstructors()
        {
            Console.WriteLine("Constructor without argument.");
            isInitialized = true;
        }
        public DefaultConstructors(int aa, int bb)
        {
            
            this.a = aa;
            this.b = bb;
            Console.WriteLine($"Constructor with two arguments: int {aa}, and int {bb}");
        }
                       
        public static void Main()
        {

            DefaultConstructors a = new DefaultConstructors();
            Console.WriteLine($"The instance of {nameof(DefaultConstructors)} is initialized: {a.isInitialized}");
            DefaultConstructors b = new DefaultConstructors(10,20);
            Console.WriteLine($"The instance of {nameof(DefaultConstructors)} is initialized with arguments {b.a}, and {b.b}");
            Console.ReadKey();
        }

    }
}
    

The output of the execution gives us the following results.

      Constructor without argument.
The instance of DefaultConstructors is initialized: True
Constructor with two arguments: int 10, and int 20
The instance of DefaultConstructors is initialized with arguments 10, and 20
    

If we do not want any modification to a class based on the arguments, we can fully omit the constructor, as it will be dynamically generated by C# at compile time.

This code also works.

      using System;
namespace Pluralsight
{
    public class DefaultConstructors
    {
       
        public static void Main()
        {

            DefaultConstructors a = new DefaultConstructors();
            Console.WriteLine($"The instance of {nameof(DefaultConstructors)} is initialized!");

            Console.ReadKey();
        }

    }
}
    

This produces the following output.

      The instance of DefaultConstructors is initialized!
    

Another feature that is very important to know about default constructors is that they initialize every numeric field to 0 and every string or object field to null.

Let's demonstrate this with the next example.

      using System;
namespace Pluralsight
{
    public class DefaultConstructors
    {
        int a;        
        string b;        
        object c;
        
        public static void Main()
        {

            DefaultConstructors a = new DefaultConstructors();
            Console.WriteLine($"The instance of {nameof(DefaultConstructors)} is initialized!");
            Console.WriteLine($"The value of a: {a.a}");
            Console.WriteLine($"The value of b: {object.Equals(a.b,null)}");
            Console.WriteLine($"The value of c: {object.Equals(a.c,null)}");
            Console.ReadKey();
        }
    }
}
    

The object.Equals() function helps us to check whether the value is null of a specific object or instance. The following output welcomes us after executing the application.

      The instance of DefaultConstructors is initialized!
The value of a: 0
The value of b: True
The value of c: True
    

The drawback of using default constructors is that you do not have the option to initialize instances differently.

There are two types of constructors at your disposal: private and public. As a general rule of thumb, remember that private constructors are used only in classes that contain only static members. If only arivate constructor exists for a class, other classes cannot create new instance of it except the nested classes.

Conclusion

In this guide we learned the most important aspects of constructors, checked out examples of how to force different behavior on your class via constructors, and demonstrated how it helps us initialize instance fields or properties with default values. The idea behind this is to save you as a programmer from common mistakes when you build your applications. I hope this guide has been informative for you and I would like to thank your for reading it.