How to Declare and Initialize Arrays in C#

How to Declare and Initialize Arrays in C#


An array is the most basic building block in the C# programming, whereby many elements of the same type are stored in a single variable. The ability to declare and initialize arrays in C# is fundamental to writing effective code. This blog will take you through how to do this step-by-step with examples.


What is an Array in C#?
An array in C# is a data structure that holds a collection of elements of the same type stored in contiguous memory locations. The elements are zero-indexed. In other words, the first element is at index 0.
Declaration of an Array
You declare an array in C# by specifying the type of elements followed by square brackets. The syntax is:

type[] arrayName;

End.

string[] names is an array declaration to store strings.

Array Initialization

You can initialize an array either at declaration time or later in your program. Here are some ways to do that:


1. Initialization with a Constant Size
You can state the size of the array while initializing it by using the new keyword:


int[] numbers = new int[5]; // declares an array of size 5

All the elements are initially set to their default values:

For integers, the default is 0.

For strings, the default is null.

For booleans, the default is false.

2. Initialization with Values

You can assign values directly to an array using curly braces {}:

int[] numbers = new int[] { 1, 2, 3, 4, 5 };

Simplified Syntax: When values are provided, you can omit the new int[] part:

int[] numbers = { 1, 2, 3, 4, 5 };

3. Initializing with a Loop

If you want to declare an array at runtime, you can do this with a for loop:


int[] numbers = new int[5];


for (int i = 0; i < numbers.Length; i)

{

numbers[i] = i + 1; // Initializes values from 1 to 5

}


4. Multidimensional Arrays

C# also supports multidimensional arrays, for example, 2D arrays:


int[,] matrix = new int[3, 3]; // A 3x3 matrix


// Assign values

matrix[0, 0] = 1;

matrix[1, 1] = 2;

matrix[2, 2] = 3;

Initializing with Values:

int[,] matrix = {

{ 1, 2, 3 },

{ 4, 5, 6 },

{ 7, 8, 9 }

};

5. Jagged Arrays
Jagged arrays are arrays of arrays where every inner array may have a different size:

int[][] jaggedArray = new int[3][];

// Assign values

jaggedArray[0] = new int[] { 1, 2 };

jaggedArray[1] = new int[] { 3, 4, 5 };

jaggedArray[2] = new int[] { 6 };

Accessing Array Elements
You can access array elements using their index:

int[] numbers = { 10, 20, 30 };

Console.WriteLine(numbers[1]); // Outputs: 20

Remember that accessing an index out of bounds will throw an exception.

More Details for programming knowledge link : 

Summary
Declaring and initializing arrays in C# is fairly simple. Here's the lowdown:

Use type[] arrayName to declare an array.

Use new and specify the size or initialize with values.


Use loops, multidimensional arrays, or jagged arrays for the complex scenarios.


Using all these techniques will let you work with data pretty efficiently in your C# applications. Play with different types of arrays to master using them!

Tags

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.