Introduction
A single-dimensional array, also known as a linear array, is the simplest form of an array. It is a list of elements of the same type, stored in contiguous memory locations. Single-dimensional arrays are useful for storing and managing collections of data where the number of elements is known and fixed.
Syntax
dataType[] arrayName = new dataType[size];
Example
int[] numbers = new int[5];
Declaring and Initializing Arrays
Arrays can be declared and initialized in several ways.
Declaring and Then Initializing
int[] numbers;
numbers = new int[5];
Declaring and Initializing Simultaneously
int[] numbers = new int[5];
Initializing with Values
int[] numbers = new int[5] { 1, 2, 3, 4, 5 };
Implicit Array Initialization
int[] numbers = { 1, 2, 3, 4, 5 };
Accessing Array Elements
Array elements are accessed using their index, starting from 0 for the first element.
Example
using System;
namespace SingleDimensionalArrayExample
{
class Program
{
static void Main(string[] args)
{
// Declare and initialize an array
int[] numbers = new int[5] { 1, 2, 3, 4, 5 };
// Access and display array elements
for (int i = 0; i < numbers.Length; i++)
{
Console.WriteLine($"Element at index {i}: {numbers[i]}");
}
// Modify an array element
numbers[2] = 10;
Console.WriteLine($"Modified element at index 2: {numbers[2]}");
}
}
}
Output
Element at index 0: 1
Element at index 1: 2
Element at index 2: 3
Element at index 3: 4
Element at index 4: 5
Modified element at index 2: 10
Array Properties and Methods
C# arrays come with several built-in properties and methods that make working with arrays easier.
Length Property
The Length
property gets the total number of elements in the array.
Example
int[] numbers = { 1, 2, 3, 4, 5 };
Console.WriteLine("Array length: " + numbers.Length);
Output
Array length: 5
Common Array Methods
Array.Sort()
: Sorts the elements of the array.Array.Reverse()
: Reverses the order of the elements in the array.Array.IndexOf()
: Finds the index of a specific element in the array.Array.Copy()
: Copies elements from one array to another.
Example
using System;
namespace ArrayMethodsExample
{
class Program
{
static void Main(string[] args)
{
int[] numbers = { 5, 2, 9, 1, 5, 6 };
// Sort the array
Array.Sort(numbers);
Console.WriteLine("Sorted array: " + string.Join(", ", numbers));
// Reverse the array
Array.Reverse(numbers);
Console.WriteLine("Reversed array: " + string.Join(", ", numbers));
// Find the index of an element
int index = Array.IndexOf(numbers, 5);
Console.WriteLine("Index of 5: " + index);
// Copy the array
int[] copiedArray = new int[numbers.Length];
Array.Copy(numbers, copiedArray, numbers.Length);
Console.WriteLine("Copied array: " + string.Join(", ", copiedArray));
}
}
}
Output
Sorted array: 1, 2, 5, 5, 6, 9
Reversed array: 9, 6, 5, 5, 2, 1
Index of 5: 2
Copied array: 9, 6, 5, 5, 2, 1
Practical Example
Let’s create a practical example where we use a single-dimensional array to store and process student grades.
using System;
namespace StudentGradesExample
{
class Program
{
static void Main(string[] args)
{
// Declare and initialize an array of student grades
int[] grades = { 85, 90, 78, 92, 88 };
// Calculate the average grade
int sum = 0;
for (int i = 0; i < grades.Length; i++)
{
sum += grades[i];
}
double average = (double)sum / grades.Length;
// Display the grades and the average
Console.WriteLine("Student Grades: " + string.Join(", ", grades));
Console.WriteLine("Average Grade: " + average);
}
}
}
Output
Student Grades: 85, 90, 78, 92, 88
Average Grade: 86.6
Conclusion
Single-dimensional arrays in C# are a fundamental way to manage collections of data. They are easy to declare, initialize, and manipulate using various array properties and methods. Understanding how to use arrays effectively is essential for working with data in C# applications. By mastering arrays, you can handle multiple values efficiently and perform a wide range of operations on them.