C# Tips and Tricks #21 – Array Initializations

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

C# Tips and Tricks #21 – Array Initializations

// 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();

Leave A Reply

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

You May Also Like

C# Compiler Error CS0442 – ‘Property’: abstract properties cannot have private accessors Reason for the Error You’ll get this error...
This is a really simple one . Below is a simple example of an enum called “Designation” defined with the...
This blog post explain the usage of the Checked Block in .NET and how you can use them in Visual...