C# - Array

An array is a data structure in programming that allows you to store a collection of elements of the same data type in contiguous memory locations. Each element in the array is accessed by its index, which represents its position within the array. Arrays provide an efficient way to manage and manipulate multiple values of the same type.

In C#, arrays are defined using square brackets [] after the data type. Here's the basic syntax to declare an array:


dataType[] arrayName = new dataType[arraySize];

Now, let's illustrate arrays with different types:

1. Integer Array (int[]):
An integer array is used to store a collection of integer values.


int[] numbers = new int[5]; // Declare an integer array with size 5

numbers[0] = 10;
numbers[1] = 20;
numbers[2] = 30;
numbers[3] = 40;
numbers[4] = 50;

2. String Array (string[]):
A string array is used to store a collection of strings.


string[] names = new string[3]; // Declare a string array with size 3

names[0] = "Alice";
names[1] = "Bob";
names[2] = "Charlie";

3. Double Array (double[]):
A double array is used to store a collection of double-precision floating-point numbers.


double[] grades = new double[4]; // Declare a double array with size 4

grades[0] = 85.5;
grades[1] = 90.0;
grades[2] = 78.3;
grades[3] = 95.2;

4. Boolean Array (bool[]):
A boolean array is used to store a collection of true/false values.


bool[] isPassed = new bool[3]; // Declare a boolean array with size 3

isPassed[0] = true;
isPassed[1] = false;
isPassed[2] = true;

5. Char Array (char[]):
A char array is used to store a collection of individual characters.


char[] letters = new char[4]; // Declare a char array with size 4

letters[0] = 'A';
letters[1] = 'B';
letters[2] = 'C';
letters[3] = 'D';

Arrays are incredibly versatile and widely used in programming for handling large sets of data, iterating through elements, and performing various operations efficiently.

Why we need of array in C# programming?

Primitive data types like int, decimal, and double can only hold single values, preventing us from storing more than one value in a single variable of such types. For instance, with int amount = 100, only one value can be stored in the int variable called 'amount.'

However, when developing software to fulfill specific business needs, there might be a requirement for 200 int type variables. Creating 200 individual variables is not recommended as it leads to code bloating and consumes a considerable amount of time.

To address this challenge, C# offers a solution by introducing Arrays. Arrays enable us to store multiple values of the same data type in a single variable. This way, we can efficiently manage a large set of int values without the need to create a multitude of individual variables.

Arrays are an essential data structure in C# programming (and in many other programming languages) because they allow you to store and organize multiple values of the same data type in a contiguous block of memory. Here are some of the key reasons why arrays are crucial in C#:
  1. Collection of Elements: Arrays provide a convenient way to store and access a collection of elements. For example, you might need to store a list of integers representing scores in a game or a list of names.
  2. Random Access: Arrays allow for efficient random access to elements using an index. Each element in the array has a unique index (starting from 0), which makes it easy to retrieve a specific element directly without iterating through the entire collection.
  3. Memory Efficiency: Arrays are memory-efficient because they store elements in contiguous memory locations. This enables the computer's memory to be used more efficiently than using individual variables for each element.
  4. Iteration and Loops: Arrays are often used with loops (e.g., for loop, foreach loop) to perform operations on all elements sequentially. This simplifies the code and makes it easier to work with large sets of data.
  5. Passing Multiple Values: When you need to pass multiple values of the same type to a method or function, using an array allows you to do so conveniently. This can help in making your code more organized and readable.
  6. Fixed Size: Arrays have a fixed size, meaning you need to define the size when declaring the array. This provides predictability in memory allocation and helps avoid potential memory issues.
  7. Performance: Arrays offer good performance for basic operations like element access, insertion, and deletion when the size is known and fixed.

Here's a simple example of how an array can be used in C#:


int[] scores = new int[5]; // Declare an array of integers with size 5

scores[0] = 85;
scores[1] = 90;
scores[2] = 78;
scores[3] = 95;
scores[4] = 88;

for (int i = 0; i < scores.Length; i++)
{
    Console.WriteLine($"Score {i+1}: {scores[i]}");
}

Output:


Score 1: 85
Score 2: 90
Score 3: 78
Score 4: 95
Score 5: 88

In summary, arrays in C# provide a practical and efficient way to work with collections of data, making it easier to manage and manipulate multiple values of the same type in your programs.