C# - Single Dimension Array

A "Single Dimension Array" in C# is like a collection of items or values that are stored in a linear sequence. Think of it as a row of storage boxes where each box can hold one item, like numbers, words, or any other type of data. You can access and manipulate these items using an index number, which represents their position in the array.

In a single-dimensional array, elements are stored in contiguous memory locations, making it easy to access individual elements using their index. The index of the first element in the array is typically 0, the second element has an index of 1, and so on.

In C#, you can make a one-dimensional array by using square brackets []. Here's the basic syntax to declare and initialize a single-dimensional array:


dataType[] arrayName = new dataType[arraySize];

dataType: The data type of the elements you want to store in the array.
arrayName: This is name of the array variable.
arraySize: The number of elements that the array can hold.

Here's an example of how to declare and initialize using an integer array with a length of 5:


Int[] evenValues = new int[5] {2,4,6,8,10};

Here's a simple example in C# to illustrate a single dimension array:


using System;

class Program
{
    static void Main()
    {
        // Declare and initialize an integer array
        int[] numbers = new int[] { 10, 20, 30, 40, 50 };

        // Access and display items in the array
        Console.WriteLine("First Number: " + numbers[0]);   // Output: First Number: 10
        Console.WriteLine("Third Number: " + numbers[2]);   // Output: Third Number: 30

        // Change the value of an item in the array
        numbers[1] = 25;

        // Display the updated item
        Console.WriteLine("Updated Second Number: " + numbers[1]);  // Output: Updated Second Number: 25
    }
}

When you run the program, you should see the following output in the console:


First Number: 10
Third Number: 30
Updated Second Number: 25

Here's what each part of the output means:

  1. First Number: 10: Displays the value of the first item in the numbers array, which is 10.
  2. Third Number: 30: Displays the value of the third item in the numbers array, which is 30.
  3. Updated Second Number: 25: After changing the value of the second item in the array from 20 to 25, this line displays the updated value, which is now 25.

Single-dimensional arrays are useful for scenarios where you need to store and work with a fixed-size collection of elements of the same data type. They are widely used in programming for handling lists of items, managing data, and performing iterative operations on data sets.

Points to Remember:
  1. Homogeneous Elements: All elements in a single-dimensional array must have the same data type. You cannot mix different data types within the same array.
  2. Fixed Size: Once you define the size of a single-dimensional array, it cannot be changed during runtime. It has a fixed length that you specify when you create the array.
  3. Zero-Based Indexing: The indexing of elements in a single-dimensional array starts from 0 for the first element, 1 for the second element, and so on, up to the size of the array minus one for the last element.
  4. Contiguous Memory: Elements in a single-dimensional array are stored in contiguous memory locations, which means they are placed one after the other in memory for efficient access.
  5. Declaration and Initialization: You can declare and initialize a single-dimensional array in one step, specifying its data type and size. Alternatively, you can declare it first and then initialize its elements later in your code.
  6. Accessing Elements: You can access individual elements in the array using square brackets [] and the index value. For example, myArray[2] would access the third element of the array.
  7. Array Length: You can find the length (number of elements) of a single-dimensional array using the .Length property. For example, myArray.Length returns the number of elements in the array.
  8. Bounds Checking: Be cautious when accessing array elements using indices. Ensure that the index is within the bounds of the array (0 to length-1) to avoid runtime exceptions like "IndexOutOfRangeException."
  9. Initialization with Values: You can initialize the elements of an array when declaring it by providing a list of values enclosed in curly braces {}. For example, int[] numbers = {1, 2, 3, 4, 5};.
  10. Arrays are Reference Types: Single-dimensional arrays in C# are reference types, meaning that when you assign an array to another variable, you are creating another reference to the same array, not a copy of the array itself.