Author avatar

Dániel Szabó

Automatic Default Values for Variables in C#

Dániel Szabó

  • Feb 24, 2020
  • 4 Min read
  • Feb 24, 2020
  • 4 Min read
Languages Frameworks and Tools


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.


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

1int a = default(int);

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

1int b = default;

Let's see this in a more complex example.

1using System;
3namespace Pluralsight
5    public class DefValues
6    {           
7        public static void Main()
8        {
9            int a = default;
10            float b = default;
11            double c = default;
12            string d = default;
13            var e = new System.Collections.ArrayList();
15            Console.WriteLine($"The variable: {nameof(a)} has value: {a}");
16            Console.WriteLine($"The variable: {nameof(b)} has value: {b}");
17            Console.WriteLine($"The variable: {nameof(c)} has value: {c}");
18            Console.WriteLine($"The variable: {nameof(d)} has value: {d}");
19            Console.WriteLine($"The variable: {nameof(e)} has value: {e}");
21            Console.ReadKey();
22        }
23    }

The output produced by the app is as follows.

1The variable: a has value: 0
2The variable: b has value: 0
3The variable: c has value: 0
4The variable: d has value:
5The 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.

1using System;
3namespace Pluralsight
5    public class DefValuesAutoProp
6    {
7        public string Name { get; set; } = default;
8        public string Age { get; set; } = default;
9        public DateTime Date {get;set; } = DateTime.Now;
11        public static void Main()
12        {
14            DefValuesAutoProp a = new DefValuesAutoProp();
15            Console.WriteLine($"The instance of {nameof(DefValuesAutoProp)} called {nameof(a)} has properties Name: {a.Name} and Age: {a.Age}");
16            Console.ReadKey();
17        }
18    }

The output produced is as follows.

1The 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.


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.