An array in C# is a collection of elements that are of the same type and are stored in contiguous memory locations. Arrays provide a way to store multiple values in a single variable. Each element in an array can be accessed using an index, and arrays are zero-indexed, meaning that the index of the first element is 0.
Characteristics of an Array
1. Fixed Size: Once an array is created, its size is fixed. This means you cannot add or remove elements after initialization.
2. Homogeneous Elements: All elements in an array are of the same data type. For example, an array of integers can only store integer values.
3. Indexed Access: Elements in an array are accessed using an index, which starts from 0. The first element is at index 0, the second at index 1, and so on.
4. Efficient Memory Allocation: Arrays store elements in contiguous memory locations, making access to array elements very efficient.
5. Zero-based Indexing: Arrays in C# are zero-based, meaning the first element of the array is at index 0.
How to Declare and Initialize Array with Values?
1. Declaring an Array: To declare an array, you use the type followed by square brackets ([]).
int[] myNumbers; // Declares an integer array
2. Creating an Array with Specified Size: You can create an array by specifying its size using the new keyword.
int[] myNumbers = new int[4]; // Creates an array of 4 integers
3. Array Initialization with Values: You can also initialize the array with values directly.
int[] myNumbers = { 10, 20, 30, 40}; // Initialize with values
4. Implicit Array Initialization: You can also omit the new int[] part and just initialize the array with values.
int[] myNumbers = { 10, 20, 30, 40}; // Initialize with values
How to access Array Element?
You can access individual elements of an array using the index of that element. Array indices in C# are zero-based, meaning the first element is accessed with index 0.
Example:
int[] myNumbers = { 10, 20, 30, 40}; // Initialize with values
Console.WriteLine(myNumbers[0]); // Output: 10 (First element)
Console.WriteLine(myNumbers[3]); // Output: 40 (Last element)
How to modify Array Elements?
You can modify an element of the array by assigning a new value to a specific index.
Example:
int[] myNumbers = { 10, 20, 30, 40}; // Initialize with values
myNumbers[3] = 50;
Console.WriteLine(myNumbers); // Output: { 10, 20, 30, 50}
Array Length
You can find the number of elements in an array using the length property.
int[] myNumbers = { 10, 20, 30, 40}; // Initialize with values
int length = myNumbers.length;
Console.WriteLine(length); // Output: 4 (Total number of elements)
How to iterate over an array?
You can use loops like for, foreach, or while to iterate over the elements of an array.
int[] myNumbers = { 10, 20, 30, 40}; // Initialize with values
foreach (int number in myNumbers)
{
Console.WriteLine(number); // Output: 10, 20, 30, 50
}
Accessing an array with an invalid index
Accessing an array with an invalid index (e.g., an index greater than or equal to the length of the array) will throw an IndexOutOfRangeException.
int[] myNumbers = { 10, 20, 30, 40}; // Initialize with values
// Will throw an exception because index 4 is out of bounds
Console.WriteLine(myNumbers[4]);