Initializing Arrays

This lesson covers initializing static, dynamic and implicitly typed arrays.

Initializing Static Arrays

An array can be declared and filled with the default value using square bracket ([]) initialization syntax. For example, creating and initializing an array of 5 integers:

Press + to interact
using System;
public class MainClass {
public static void Main(String [] args)
{
int[] arr = { 24, 2, 13, 47, 45 };
foreach(var item in arr)
{
Console.WriteLine(item.ToString());
}
}
}

Example Explanation

In the above example, in line 7 we have an array arr. We don’t need to specify it’s size - the compiler will automatically know that it’s an array of size 5 ...