Author avatar

Dániel Szabó

Default Constructors and Their Invocation in C#

Dániel Szabó

  • Feb 11, 2020
  • 5 Min read
  • 403 Views
  • Feb 11, 2020
  • 5 Min read
  • 403 Views
Languages Frameworks and Tools
C#

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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();
        }

    }
}
csharp

The output of the execution gives us the following results.

1
2
3
4
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
bash

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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();
        }

    }
}
csharp

This produces the following output.

1
The instance of DefaultConstructors is initialized!
bash

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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();
        }
    }
}
csharp

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.

1
2
3
4
The instance of DefaultConstructors is initialized!
The value of a: 0
The value of b: True
The value of c: True
bash

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.

4