How to define a single-dimensional array in C Sharp?

An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type stored at contiguous memory locations.

A single-dimensional array in C# is a linear collection of elements of the same data type. It allows you to store multiple values in a single variable and access them using an index.

Syntax

Following is the syntax for declaring a single-dimensional array −

datatype[] arrayName = new datatype[size];

Following is the syntax for initializing an array with values −

datatype[] arrayName = new datatype[size] {value1, value2, ...};

You can also use simplified initialization syntax −

datatype[] arrayName = {value1, value2, value3};

Array Declaration and Initialization

To define a single-dimensional array without initialization −

int[] runs = new int[10];

To initialize the array in the same line −

int[] runs = new int[5] {125, 173, 190, 264, 188};

Single-Dimensional Array Structure Index: Value: 0 1 2 3 4 125 173 190 264 188 runs[0] runs[1] runs[2] runs[3] runs[4] Array elements are accessed using zero-based indexing

Example

The following example demonstrates how to declare, initialize and display an array −

using System;

namespace Program {
   class Demo {
      static void Main(string[] args) {
         int[] runs = new int[5] {125, 173, 190, 264, 188};
         int j;
         for (j = 0; j < 5; j++) {
            Console.WriteLine("Innings score of Cricketer[{0}] = {1}", j, runs[j]);
         }
      }
   }
}

The output of the above code is −

Innings score of Cricketer[0] = 125
Innings score of Cricketer[1] = 173
Innings score of Cricketer[2] = 190
Innings score of Cricketer[3] = 264
Innings score of Cricketer[4] = 188

Different Ways to Initialize Arrays

Using Simplified Syntax

using System;

class Program {
   static void Main() {
      string[] cities = {"New York", "London", "Tokyo", "Paris"};
      
      Console.WriteLine("Cities in the array:");
      for (int i = 0; i < cities.Length; i++) {
         Console.WriteLine("{0}. {1}", i + 1, cities[i]);
      }
   }
}

The output of the above code is −

Cities in the array:
1. New York
2. London
3. Tokyo
4. Paris

Using Array.Length Property

using System;

class Program {
   static void Main() {
      double[] prices = {19.95, 25.50, 12.75, 8.99, 15.25};
      
      Console.WriteLine("Array Length: " + prices.Length);
      Console.WriteLine("Product Prices:");
      
      for (int i = 0; i < prices.Length; i++) {
         Console.WriteLine("Product {0}: ${1}", i + 1, prices[i]);
      }
   }
}

The output of the above code is −

Array Length: 5
Product Prices:
Product 1: $19.95
Product 2: $25.5
Product 3: $12.75
Product 4: $8.99
Product 5: $15.25

Conclusion

Single-dimensional arrays in C# provide an efficient way to store multiple values of the same type in contiguous memory locations. They use zero-based indexing and can be initialized in multiple ways, making them fundamental data structures for managing collections of related data.

Updated on: 2026-03-17T07:04:35+05:30

361 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements