Free Web Development Online Tutorials, Learn to Code
c# array declaration, c# array methods, C# List, c# array and string, c# an array of lists

C# Array Example

In this tutorial you will learn C# Array with example, also different between C# Array and ArrayList.

C# Array and ArrayList

Array and ArrayList both are used to store collection of object, but there are some different, in real-time application development you come across situation where you need to decide which one to use, both are very useful.

What is an Array in C#?
An Array is a strongly-typed collection that store a series of elements same type

using system;
public abstract class Array : ICloneable, IList, ICollection, IEnumerable, IStructuralComparable, IStructuralEquatable
Integer Array c# example

this is how you initialize an integer array and add item to an Array.
C# Arrays initialization and c# array add item example

int[] myArray=new int[]{5};
myArray[0] = 1;
myArray[1] = 2;
myArray[2] = 3;

There is another alternate way of creating integer array, you can create a list object, then add item to list object, and finally you can get an array just by converting list to array

List to Array Example
List<int> objInList = new List<int>();

int[] intArray = objInList.ToArray();
String Array C# Example

this is how you initialize an string array.
C# array add new item : adding new item to string Array

string[] myArray=new string[4];
myArray[0] = "Red";
myArray[1] = "White";
myArray[2] = "Green";
myArray[3] = "Blue";

To read values from an Array, you can simply use the index number or you can loop through array object.

string _value = myArray[2]; 

// OR
foreach (string s in myArray)

As you can see in above int or string array we could add only that particular data type like int or string.

C# Array Methods Example

Here are some important methods of System.Array class

  • Copy(Array,Array,Int32)

    Copy elements of an array into another array by specifying starting index

  • CopyTo(Array,Int32)

    Copies all the elements of the current one-dimensional array to the specified one-dimensional array

  • Clone()

    Copy array structure without data, Create a shallow copy of the Array

  • Reverse(Array)

    Reverse the sequence of the elements in the entire one-dimensional Array

    Here is an example of string array Reverse method in c#

    string[] myArray = new string[4] ;
    myArray[0] = "Red";
    myArray[1] = "White";
    myArray[2] = "Green";
    myArray[3] = "Blue";
    foreach (string s in myArray)
  • Sort(Array): Sort an Array

    Sort the elements in an entire one-dimensional Array

    Here is an example of string array Sort method in c#

    string[] myArray = new string[4] ;
    myArray[0] = "Red";
    myArray[1] = "White";
    myArray[2] = "Green";
    myArray[3] = "Blue";
    foreach (string s in myArray)
C# Array Properties
  • Length

    Get the total number of elements in that Array

  • IsFixedSize

    Get bool value indicating whether the Array has a fixed size or not.

  • IsReadOnly

    Boolean value indicates if the Array is read-only

What is an ArrayList in C#?

An ArrayList is not a strongly-typed collection. It can store the values of different data types or same data type. The size of an array list can increases or decreases dynamically, it can take any size of values from any data type.

ArrayList myArrayList = new ArrayList();
myArrayList.Add("this is string");

As you can see in above ArrayList we have added different data types integer, string, decimal, date etc

equilibrium index of an array

Sum of elements at lower indexes = Sum of elements at higher indexes.

int[] arr = { -9, 2, 5, 4, -2, 3, -3 };

3 is an equilibrium index of above array, because

arr[0] + arr[1] + arr[2] = arr[4] + arr[5] + arr[6]

Third element of the array arr[3] which is the balance point of both side of the array.

Here is how you can find equilibrium index of an array!

int getEquilibriumIndex(int[] arr)
int arrLength= arr.Length;
int i, j;
int leftSum, rightSum;
for (i = 0; i < arrLength; ++i)
leftSum = 0;
rightSum = 0;
for (j = 0; j < i; j++)
leftSum += arr[j];
for (j = i + 1; j < arrLength; j++)
            rightSum += arr[j];

        if (leftSum == rightSum)
            return i;

    return -1;

// Calling the function.
int[] arr = { -9, 2, 5, 4, -2, 3, -3 };
int result = getEquilibriumIndex(arr);

Difference between C# Array and ArrayList

C# Array C# ArrayList
Arrays belong to System.Array namespace using System; Arraylist belongs to System.Collection namespaces using System.Collections;
In Array, we can store only one datatype either int, char, string etc In Arraylist, we can store different datatype values
Array cant accept null ArrayList collection accepts null
.Net C# Examples | Join .Net C# Course