Skip to content

Contact sales

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

Automatic Default Values for Variables in C#

Languages like C# require developers to declare their variables beforehand, so let's see what you need to watch out for when using different types.

Feb 24, 2020 • 4 Minute Read

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.

      int a = default(int);
    

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

      int b = default;
    

Let's see this in a more complex example.

      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();
        }
    }
}
    

The output produced by the app is as follows.

      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
    

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.

      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();
        }
    }
}
    

The output produced is as follows.

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

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.