0
0
C Sharp (C#)programming~5 mins

Single-dimensional array declaration in C Sharp (C#)

Choose your learning style9 modes available
Introduction

Arrays help you store many values of the same type in one place. A single-dimensional array is like a row of boxes where each box holds one value.

When you want to keep a list of numbers, like scores in a game.
When you need to store names of students in a class.
When you want to hold daily temperatures for a week.
When you want to process a fixed number of items one by one.
When you want to loop through a collection of items easily.
Syntax
C Sharp (C#)
public class SingleDimensionalArrayExample
{
    public static void Main()
    {
        // Declare an array of integers with 5 elements
        int[] numbers = new int[5];

        // Declare and initialize an array of strings
        string[] names = { "Alice", "Bob", "Charlie" };
    }
}

The number inside the square brackets [] defines the size of the array.

You can declare and initialize an array at the same time or separately.

Examples
This creates an empty array with zero elements.
C Sharp (C#)
int[] emptyArray = new int[0];
An array with one element. You can assign a value to the first (and only) box.
C Sharp (C#)
int[] singleElementArray = new int[1];
singleElementArray[0] = 42;
Declare and fill the array with 3 fruit names right away.
C Sharp (C#)
string[] fruits = { "Apple", "Banana", "Cherry" };
Assign values to the first and last elements in a 5-element array.
C Sharp (C#)
int[] numbers = new int[5];
numbers[0] = 10;
numbers[4] = 50;
Sample Program

This program shows how to declare a single-dimensional array, print its default values, assign new values, and print them again.

C Sharp (C#)
using System;

public class SingleDimensionalArrayExample
{
    public static void Main()
    {
        // Declare and initialize an array of 5 integers
        int[] scores = new int[5];

        Console.WriteLine("Scores before assignment:");
        for (int index = 0; index < scores.Length; index++)
        {
            Console.WriteLine($"scores[{index}] = {scores[index]}");
        }

        // Assign values to the array
        scores[0] = 85;
        scores[1] = 90;
        scores[2] = 78;
        scores[3] = 92;
        scores[4] = 88;

        Console.WriteLine("\nScores after assignment:");
        for (int index = 0; index < scores.Length; index++)
        {
            Console.WriteLine($"scores[{index}] = {scores[index]}");
        }
    }
}
OutputSuccess
Important Notes

Time complexity to access or assign an element by index is O(1) - very fast.

Space complexity is proportional to the number of elements you declare.

Common mistake: Accessing an index outside the array size causes an error.

Use arrays when you know the number of elements in advance and want fast access by position.

Summary

Single-dimensional arrays store multiple values of the same type in a simple list.

You declare them by specifying the type, square brackets, and size or initial values.

Access elements by their position using zero-based indexes.