Single-dimensional array is the simplest array in C#. A single-dimensional array stores all elements in a single row. 

Syntax and array decleration

An array can be declared by specifying its type name followed by square brackets ([]).

datatype[] ArrayName;
  • datatype is the type of the elements of the array.
  • Size of the array can be specified in the [].
  • ArrayName is the name of the array.

Array initialization

int[] IntArray = new int[3] {10, 11};
char[] CharArray = new char[2] {'a', 'b'};

Assigning values

The value of any array element can be changed using its index.

char[] CharArray = new char[2] {'a', 'b'};
CharArray[1] = 'c';
//This will replace b with c

Accessing array elements

Array elements can be accessed using any of the below mentioned methods:

Using for loop:

using System;   //Adding namespaces
namespace LearnCSharp //Namespace of the class
{
class Program //The class
{
static void Main(string[] args) //Main method
{
//Declarig and Initializing array
int[] IntArray = new int[2] {10, 11};

//length of the array
int l = IntArray.Length;
for (int i = 0; i < IntArray.Length; i++ )
{
Console.WriteLine("Element at index {0}: {1}", i, IntArray[i]);
}
Console.ReadKey();
}
}
}

Output

Element at index 0: 10
Element at index 1: 11

Using foreach loop:

class Program   //The class
{
static void Main(string[] args) //Main method
{
//Declarig and Initializing array
char[] CharArray = new char[2] { 'a', 'b'};

foreach(char i in CharArray)
{
Console.WriteLine("Element : {0}", i);
}
Console.ReadKey();
}
}

Output

Element : a
Element : b

By specifying index:

static void Main(string[] args) //Main method
{
//Declarig and Initializing array
double[] DoubleArray = new double[2] { 3.14, 7.5};
Console.WriteLine("The element is: {0}", DoubleArray[1]);
Console.ReadKey();
}

Output – The element is: 7.5