Languages
[Edit]
EN

C# / .NET - default array values

0 points
Created by:
Elleanor-Williamson
380

In this article, we would like to show you what are default array values initialized by the compiler when you don’t assign values to array elements in C#.

Quick solution:

DatatypeDefault value

DatatypeDefault value
int0
double0.0
booleanfalse
char'\0' (U+0000)
stringnull
user-definednull

 

Practical example

In this example, we create one-element, not initialized arrays to see what is the default value for each type.

using System;

public class Program
{
    public static void Main()
    {
        int[] array1 = new int[1];
        double[] array2 = new double[1];
        bool[] array3 = new bool[1];

        Console.WriteLine("int: " + array1[0]);
        Console.WriteLine("double: " + array2[0]);
        Console.WriteLine("bool: " + array3[0]);
    }
}

Output:

int: 0
double: 0
bool: False

References

  1. Default values of C# types - C# reference | Microsoft Docs
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join