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 |
In this example, we create one-element, not initialized arrays to see what is the default value for each type.
xxxxxxxxxx
1
using System;
2
3
public class Program
4
{
5
public static void Main()
6
{
7
int[] array1 = new int[1];
8
double[] array2 = new double[1];
9
bool[] array3 = new bool[1];
10
11
Console.WriteLine("int: " + array1[0]);
12
Console.WriteLine("double: " + array2[0]);
13
Console.WriteLine("bool: " + array3[0]);
14
}
15
}
Output:
xxxxxxxxxx
1
int: 0
2
double: 0
3
bool: False