Arrays are one of the most essential data structures in programming, and in C#, they offer an effective way to manage and manipulate multiple items of the same data type. A single-dimensional array, often referred to simply as an array, is the simplest form of array that holds a sequence of elements in a single line. Understanding how to work with single-dimensional arrays can streamline code and make handling data easier in various applications.
In C#, a single-dimensional array is a fixed-size collection of elements, all of the same type, stored in consecutive memory locations. The elements are accessed through indices, which start at 0. For example, an array of integers [10, 20, 30]
has three elements at positions 0, 1, and 2.
Single-dimensional arrays are beneficial when:
There are multiple ways to declare and initialize single-dimensional arrays in C#.
// Declaring an array with a specified size but no values yet
int[] numbers = new int[5];
// Declaring and initializing an array with values
int[] numbers = new int[] {10, 20, 30, 40, 50};
// Simplified declaration with values
int[] numbers = {10, 20, 30, 40, 50};
In each case:
int[]
specifies the type of array.int[5]
creates an array of five integers, each initialized to 0 by default.{}
initializes the array with specific values.To access an element in a single-dimensional array, use its index inside square brackets []
. Remember, the index of the first element is 0, and it increments by 1 for each subsequent element.
int[] numbers = {10, 20, 30, 40, 50};
// Access the first element
Console.WriteLine(numbers[0]); // Output: 10
// Access the last element
Console.WriteLine(numbers[4]); // Output: 50
// Modify an element
numbers[2] = 35;
Console.WriteLine(numbers[2]); // Output: 35
Attempting to access an index outside the bounds of the array (e.g., numbers[5]
in this example) will result in an IndexOutOfRangeException
.
Single-dimensional arrays in C# support a variety of operations that make it easy to handle and manipulate data.
You can iterate through an array using a for
loop or foreach
loop.
for
Loopint[] numbers = {10, 20, 30, 40, 50};
for (int i = 0; i < numbers.Length; i++)
{
Console.WriteLine(numbers[i]);
}
foreach
Loopforeach (int num in numbers)
{
Console.WriteLine(num);
}
int[] numbers = {10, 20, 30, 40, 50};
Console.WriteLine(numbers.Length); // Output: 5
To sort an array in ascending order:
Array.Sort(numbers);
To reverse the order of elements in an array:
Array.Reverse(numbers);
Let’s consider a scenario where a teacher wants to store and analyze the grades of students in a class. A single-dimensional array can store these grades, making it easy to calculate the average grade, highest grade, or lowest grade.
We have a list of student grades, and we want to:
We can use a single-dimensional array to store the grades, then use a loop to calculate the required values.
class GradeAnalysis
{
static void Main(string[] args)
{
int[] grades = {85, 92, 78, 90, 88};
// Calculate the highest grade
int highestGrade = grades[0];
for (int i = 1; i < grades.Length; i++)
{
if (grades[i] > highestGrade)
{
highestGrade = grades[i];
}
}
// Calculate the average grade
int sum = 0;
foreach (int grade in grades)
{
sum += grade;
}
double averageGrade = (double)sum / grades.Length;
Console.WriteLine($"Highest Grade: {highestGrade}");
Console.WriteLine($"Average Grade: {averageGrade}");
}
}
grades
array to store the grades of students.Highest Grade: 92
Average Grade: 86.6
In this tutorial, we explored single-dimensional arrays in C# and learned how to:
Single-dimensional arrays are foundational for data storage in C#. They’re widely used in applications that involve organizing a collection of similar data types in a fixed sequence, from simple lists to more complex structures like matrix rows. Mastering arrays will provide a solid basis for more advanced data manipulation tasks in C#.