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.
Single-dimensional array declaration in 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.
int[] emptyArray = new int[0];
int[] singleElementArray = new int[1]; singleElementArray[0] = 42;
string[] fruits = { "Apple", "Banana", "Cherry" };int[] numbers = new int[5]; numbers[0] = 10; numbers[4] = 50;
This program shows how to declare a single-dimensional array, print its default values, assign new values, and print them again.
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]}"); } } }
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.
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.