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

This C# program calculates and displays an upper triangular matrix based on user input. Problem Statement: The program takes the...
This C# program serves as a demonstration of bitwise operators, which are fundamental operators used for manipulating individual bits in...
This C# program is designed to interchange or swap the columns of a matrix. A matrix is a two-dimensional array...