Single Dimensional Array in C# with Example

When diving into the world of C#.Net programming, one of the fundamental data structures you’ll come across is the array. Arrays are immensely useful for storing and organizing a series of items sequentially. In this blog post, we’ll delve deep into the single-dimensional array in or one-dimensional array in c# with examples and understand its uses, syntax, and operations through a hands-on example.

What is a Single Dimensional Array in C#?

A single dimensional array, also referred to as a one-dimensional array, is essentially a list of items of the same type in C#. Think of it as a single row or a column in a table, where each cell in that row (or column) can store a data item. In C#, arrays are zero-indexed, meaning the first item is accessed using index 0.

Declaring a Single-Dimensional Array in C#

Declaration of a single-dimensional array in C# is straightforward. The basic syntax is as follows:

dataType[] arrayName;

For instance, to declare an integer array in C#.Net:

int[] numbers;

Initializing a Single-Dimensional Array in C#

There are several methods to initialize a one-dimensional array in C#:

  1. At the time of declaration:
int[] numbers = new int[5];

This creates an integer array with a size of 5, but with all elements uninitialized.

  1. Declaration with initialization:
int[] numbers = {1, 2, 3, 4, 5};

Here, the array is initialized with 5 integer values.

Accessing Array Elements

You can access elements in the array by referring to the index number. Remember, arrays in C# are zero-indexed.

int firstNumber = numbers[0]; // Accesses the first element of the array.

Single Dimensional Array in C# with Example

Let’s see a simple example that demonstrates the use of a single-dimensional array in C#.Net:

using System;

namespace SingleDimensionArrayExample
    class Program
        static void Main(string[] args)
            // Declare and initialize the array
            int[] numbers = {10, 20, 30, 40, 50};
            // Print each number using a loop
            for (int i = 0; i < numbers.Length; i++)
                Console.WriteLine($"Element at index {i} is: {numbers[i]}");

When you run the above program, it will display each element of the array and its corresponding index. You can check the output below:

one dimensional array in c# with example


In this C#.Net tutorial, we discussed, a one-dimensional array in c# with examples or a single-dimensional array in c# with examples.

You may also like: