C# Single-dimensional array


A single-dimensional array in C# is a data structure that stores a fixed-size collection of elements of the same type, organized in a single row. The elements of the array are accessed using a zero-based index, meaning that the index of the first element is 0, the second element is 1, and so on.

Key Features of Single-Dimensional Arrays

  1. Fixed Size: Once an array is created, its size cannot be changed. The size is specified during the array's creation.
  2. Homogeneous Elements: All elements of the array must be of the same type.
  3. Indexed Access: Array elements are accessed using an index, which starts at 0.
  4. Contiguous Memory: Arrays are stored in contiguous memory locations, which allows for efficient access to elements using their index.

Syntax for Declaring a Single-Dimensional Array

To declare a single-dimensional array in C#, you use the following syntax:

dataType[] arrayName;
  • dataType specifies the type of elements that the array will store.
  • arrayName is the name of the array.

Creating and Initializing an Array

There are different ways to create and initialize a single-dimensional array in C#.

  1. Declaring and then initializing the array:
int[] numbers = new int[5]; // Creates an array of 5 integers (default value is 0)
  1. Declaring, initializing, and assigning values:
int[] numbers = new int[] { 1, 2, 3, 4, 5 }; // Creates and initializes the array
  1. Shorthand initialization:
int[] numbers = { 1, 2, 3, 4, 5 }; // Implicitly defines the array size based on the provided elements

Accessing and Modifying Array Elements

Array elements are accessed using their index. The index is zero-based, so the first element is at index 0, the second element is at index 1, and so on.

Example:

using System; public class Program { public static void Main() { // Declare and initialize an array int[] numbers = { 10, 20, 30, 40, 50 }; // Accessing elements Console.WriteLine("First element: " + numbers[0]); // Output: 10 Console.WriteLine("Second element: " + numbers[1]); // Output: 20 // Modifying elements numbers[2] = 35; // Changing the third element // Accessing the modified element Console.WriteLine("Modified third element: " + numbers[2]); // Output: 35 } }

Array Properties and Methods

C# provides several properties and methods for working with arrays:

  • Length: Returns the total number of elements in the array.

    int length = numbers.Length; // Output: 5
  • GetValue(int index): Returns the value at the specified index.

    int value = (int)numbers.GetValue(0); // Output: 10
  • SetValue(object value, int index): Sets a value at the specified index.

    numbers.SetValue(25, 1); // Changes the second element to 25
  • Sort(): Sorts the array in ascending order.

    Array.Sort(numbers);
  • Reverse(): Reverses the elements of the array.

    Array.Reverse(numbers);

Iterating Over an Array

You can use a for loop or a foreach loop to iterate over the elements of an array.

Example of a for loop:

for (int i = 0; i < numbers.Length; i++) { Console.WriteLine(numbers[i]); }

Example of a foreach loop:

foreach (int number in numbers) { Console.WriteLine(number); }

Example of Using a Single-Dimensional Array

using System; public class Program { public static void Main() { // Declare and initialize an array string[] fruits = { "Apple", "Banana", "Cherry", "Date", "Elderberry" }; // Access and modify elements Console.WriteLine("First fruit: " + fruits[0]); fruits[1] = "Blueberry"; // Change the second element // Iterate using foreach Console.WriteLine("Fruits list:"); foreach (string fruit in fruits) { Console.WriteLine(fruit); } // Check the length of the array Console.WriteLine("Total number of fruits: " + fruits.Length); } }

Summary

  • A single-dimensional array is a fixed-size collection of elements, where each element can be accessed using an index.
  • Arrays in C# are zero-indexed, meaning the first element is at index 0.
  • You can declare, initialize, access, and modify arrays easily using [].
  • Arrays come with several built-in properties and methods, such as Length, Sort(), and Reverse().