C# Tips and Tricks #21 – Array Initializations

Here’s the list of some of the possible ways of array initializations in C#

// Array Initializations in C#
//1. Creates an array if 2 items with default values
string[] array1 = new string[2]; 

//2. Creates an array of 2 items with 2 values
string[] array2 = new string[] { "Senthil", "Balu" };

//3. Creates an array of 2 items with 2 values
string[] array3 = { "Senthil", "Balu" }; 

//4. Creates an array of 2 items
string[] array4 = new[] { "Senthil", "Balu" };


//5. Initialize Array with a fixes array if pre-initialized values (Empty strings)
var array = Enumerable.Repeat(string.Empty, 10).ToArray();
Pin It

Leave a Reply

Your email address will not be published. Required fields are marked *