Welcome to Lesson 8! In this lesson, we’ll delve into arrays, a crucial data structure in C# used for storing and managing collections of elements. Arrays are foundational for handling multiple values of the same type efficiently. We’ll cover the basics of arrays, including how to declare, initialize, and work with them.
1. What is an Array?
An array is a data structure that holds a fixed-size sequence of elements of the same type. Each element in an array is identified by an index, which starts at 0.
2. Declaring and Initializing Arrays
To use an array, you first need to declare it and then initialize it with a specific size or values.
Syntax:
type[] arrayName = new type[size];
Example:
int[] numbers = new int[5]; // Array to hold 5 integers
Explanation:
- This creates an integer array named
numberswith a capacity to hold 5 elements. By default, all elements are initialized to0.
Initializing Arrays with Values:
You can also initialize an array with values directly.
Syntax:
type[] arrayName = { value1, value2, value3, ... };
Example:
string[] fruits = { "Apple", "Banana", "Cherry" };
Explanation:
- This creates a
fruitsarray with three elements: “Apple”, “Banana”, and “Cherry”.
3. Accessing Array Elements
Array elements are accessed using their index, which is zero-based.
Example:
string[] fruits = { "Apple", "Banana", "Cherry" };
Console.WriteLine(fruits[0]); // Output: Apple
Console.WriteLine(fruits[1]); // Output: Banana
Console.WriteLine(fruits[2]); // Output: Cherry
Explanation:
fruits[0]refers to the first element,fruits[1]to the second, andfruits[2]to the third.
4. Array Length
The Length property of an array returns the number of elements in the array.
Example:
int[] numbers = { 10, 20, 30, 40, 50 };
Console.WriteLine("Array length: " + numbers.Length); // Output: 5
Explanation:
numbers.Lengthreturns the total number of elements in thenumbersarray.
5. Iterating Through Arrays
You often need to process each element of an array. Loops are commonly used to iterate through arrays.
Example Using for Loop:
int[] numbers = { 1, 2, 3, 4, 5 };
for (int i = 0; i < numbers.Length; i++)
{
Console.WriteLine(numbers[i]);
}
Explanation:
- The
forloop iterates over each index from0tonumbers.Length - 1, printing each element.
Example Using foreach Loop:
int[] numbers = { 1, 2, 3, 4, 5 };
foreach (int number in numbers)
{
Console.WriteLine(number);
}
Explanation:
- The
foreachloop provides a more concise way to iterate through the elements of the array without managing the index.
6. Modifying Array Elements
You can modify elements in an array by assigning new values to specific indices.
Example:
int[] numbers = { 1, 2, 3, 4, 5 };
numbers[2] = 10; // Change the value at index 2
foreach (int number in numbers)
{
Console.WriteLine(number);
}
Explanation:
- The value at index 2 is changed from 3 to 10. The
foreachloop then prints the updated array.
7. Array Initialization with Default Values
When you create an array, its elements are automatically initialized to the default value for their type. For numeric types, this default is 0; for bool, it’s false; and for reference types, it’s null.
Example:
int[] defaultArray = new int[3]; // Elements are initialized to 0
foreach (int number in defaultArray)
{
Console.WriteLine(number); // Output: 0 0 0
}
Explanation:
- The
defaultArrayelements are initialized to0by default.
8. Example: Finding the Sum of Array Elements
Here’s an example of calculating the sum of elements in an array.
Example:
int[] numbers = { 1, 2, 3, 4, 5 };
int sum = 0;
for (int i = 0; i < numbers.Length; i++)
{
sum += numbers[i];
}
Console.WriteLine("Sum of elements: " + sum); // Output: 15
Explanation:
- The
forloop adds each element of thenumbersarray to thesumvariable.
9. Example: Reversing an Array
Let’s see how to reverse the elements of an array.
Example:
int[] numbers = { 1, 2, 3, 4, 5 };
Array.Reverse(numbers);
foreach (int number in numbers)
{
Console.WriteLine(number); // Output: 5 4 3 2 1
}
Explanation:
- The
Array.Reversemethod reverses the order of elements in thenumbersarray.
Summary
In this lesson, you learned about arrays in C#, including how to declare, initialize, and work with arrays. We covered accessing and modifying elements, iterating through arrays with loops, and provided examples for finding the sum and reversing array elements. Arrays are essential for managing collections of data and are foundational for many programming tasks.
In the next lesson, we’ll explore multidimensional arrays, which extend the concept of arrays to handle more complex data structures.
Happy coding!

0 Comments