EN
C# / .NET - default array values
0
points
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
| Datatype | Default value |
|---|---|
| int | 0 |
| double | 0.0 |
| boolean | false |
| char | '\0' (U+0000) |
| string | null |
| user-defined | null |
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