Free Web Development Online Tutorials, Learn to Code
Array, ArrayList in C#, Learn C# programming Language, C# List, c# array methods example

C# Array example, Array Methods

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

Arrays and ArrayList in C#

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>();
objInList.Add(10);
objInList.Add(15);
objInList.Add(18);
objInList.Add(21);
objInList.Add(32);

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)
{
    Console.WriteLine(s);
}

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

C# arrays 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";
    Array.Reverse(myArray);
    foreach (string s in myArray)
    {
        Console.WriteLine(s);
    }
  • 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";
    Array.Sort(myArray);
    foreach (string s in myArray)
    {
        Console.WriteLine(s);
    }
    
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(1);
myArrayList.Add("this is string");
myArrayList.Add(1.2);
myArrayList.Add(DateTime.Now);

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

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
Comment
Name
Email
Website
Subscribe
 

Learn Array , ArrayList
in C# Tutorial
Other Online Tutorial

C# .net Interview Questions Answers