Author avatar

Dániel Szabó

Automatic Default Values for Variables in C#

Dániel Szabó

  • Feb 24, 2020
  • 4 Min read
  • 2,107 Views
  • Feb 24, 2020
  • 4 Min read
  • 2,107 Views
Languages Frameworks and Tools
C#

Introduction

Statically typed languages like C# require developers to declare their variables beforehand, and the compiler takes care of initialization during compile time. The question this guide will answer is what those variables are initialized with and what you need to watch out for when using different types. We'll also dive into auto properties, as they are distant relatives to variables, and we may look at a default value, too.

Variable Categories

There are three distinct types of variables that are automatically initialized with a default value based on their type.

  1. Static variables
  2. Instance variables of class instances
  3. Array elements
  4. Reference type (a.k.a. pointer)

Except for reference types, which are initialized with null, the rest are initialized with a value provided by their default constructor.

The initialization to default values is done behind the scenes either by the memory manager or the garbage collector, and all bits are set to zero. All bits set to zero means something different for int, float or string.

Default Values Per Types

  1. Any reference type: null
  2. Any built-in integral numeric: 0
  3. Any built-in floating-point numeric: 0
  4. bool: false
  5. char: \0
  6. enum: The value produced by the expression
  7. struct: All value-type fields are set to their default values.
  8. Any nullable value type: The null value of a nullable value type.

Initialization

Before C# version 7.1, you could use the default operator to initialize a variable like this.

1
int a = default(int);
csharp

After version 7.1, the default literal can be used to initialize a variable.

1
int b = default;
csharp

Let's see this in a more complex example.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
using System;

namespace Pluralsight
{
    public class DefValues
    {           
        public static void Main()
        {
            int a = default;
            float b = default;
            double c = default;
            string d = default;
            var e = new System.Collections.ArrayList();

            Console.WriteLine($"The variable: {nameof(a)} has value: {a}");
            Console.WriteLine($"The variable: {nameof(b)} has value: {b}");
            Console.WriteLine($"The variable: {nameof(c)} has value: {c}");
            Console.WriteLine($"The variable: {nameof(d)} has value: {d}");
            Console.WriteLine($"The variable: {nameof(e)} has value: {e}");

            Console.ReadKey();
        }
    }
}
csharp

The output produced by the app is as follows.

1
2
3
4
5
The variable: a has value: 0
The variable: b has value: 0
The variable: c has value: 0
The variable: d has value:
The variable: e has value: System.Collections.ArrayList
bash

nameof can be used to get the name of a specific variable, class, struct etc.

Another example is auto-properties in C#. When you use properties to describe your class's internal state, it is important to tie up loose ends. We can do this by simply initializing properties with default values. This feature was included in C# with the 6.0 release.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
using System;

namespace Pluralsight
{
    public class DefValuesAutoProp
    {
        public string Name { get; set; } = default;
        public string Age { get; set; } = default;
        public DateTime Date {get;set; } = DateTime.Now;

        public static void Main()
        {

            DefValuesAutoProp a = new DefValuesAutoProp();
            Console.WriteLine($"The instance of {nameof(DefValuesAutoProp)} called {nameof(a)} has properties Name: {a.Name} and Age: {a.Age}");
            Console.ReadKey();
        }
    }
}
csharp

The output produced is as follows.

1
The instance of DefValuesAutoProp called a has properties Name:  and Age:  and Date: 2/17/2020 11:48:05 AM
bash

You may even set default values for read-only auto-properties.

Conclusion

In this guide, we have learned how to utilize the default value assignment capabilities of C#. We have also seen that class properties can be initialized with this feature as well. I hope this guide has been informative to you and I would like to thank you for reading it.

15