Arrays In C#

An array stores a fixed-size sequential collection of elements of the same type. We can use them to gather items in a single group, and perform various operations on them.

Arrays are of the following three types in C#.

  1. One Dimensional Array

    Syntax:

    DataType[] NameOfArray = new DataType[Number of Elements(Size)];

    For Example:
    1. Int[] Arr = new int[5];  
    In the above example we have created one dimension array of Type int and having size 5.

  2. Two Dimensional Array

    Syntax:

    DataType[,] NameOfArray = new DataType[Sze of Rows, Size of Column];

    For Example:
    1. Int[,] Arr = new int[5,5];  
    In the above example we have created one dimension array of Type Integer.

    Two Dimensional means the array will look like a table with fixed numbers of rows and columns declared in syntax.

  3. Jagged Array

    Syntax:

    DataType[][] NameOfArray = new DataType[Number of Elements(Size)][];

    For Example:
    1. Int[][] Arr = new int[5][];  
    Jagged Arrays are basically collection of arrays. In the case of jagged array every Element of an array contains array.

Advantages

There are some advantages of using Arrays.

  • We can create an array instead of creating multiple Variable for the same Memory Location.
  • We can store n-numbers of elements in an array.
  • Elements of an array are stored with the sequential index order so, it is easy to find any element with their index number directly.

Drawback

  • Size (numbers of elements to be stored) is fixed at Compile Time or at the time of initialization of it.

This is the basic concept of creating arrays in C#. You will get the process to insert/select the element using loops and index in an array in my next blog.