How To Initialize Array In C#

Sameer Saini March 24, 2022
How To Initialize Array In C#

As we know an Array in C# is a collection of objects. This Array could be of any type, example, a string array or an int Array.

Before we can use an Array and do something with it like assign values or append values or iterate on it, we need to initialize the array.

So, in this blog post we will see how to initialize an Array in C#.

 

 

How To Initialize Array In C# With A Fixed Length

// Initialize a new string array with a length of 5 string[] colors = new string[5]; // Initialize a new int array with a length of 5 int[] numbers = new int[5];

 

 

How To Initialize Array In C# With Default Values In The Array

// Initialize a new string array with default values string[] colors = new string[] { "red", "orange", "green"}; // or string[] colors_1 = { "red", "orange", "green" }; // Initialize a new int array with default values int[] numbers = new int[] { 1, 2, 3, 4, 5 }; // or int[] numbers_1 = { 1, 2, 3, 4, 5 };