articles

Home / DeveloperSection / Articles / what is an array in c#

what is an array in c#

what is an array in c#

Ravi Vishwakarma963 04-Jul-2021

What is array in c#

 We can store multiple value in one variable but it’s treated as multiple variable. You can declare an array using square bracket “[ ]”. This bracket says that this is array. This is support the homogeneous type element in other words. An array can be single dimension, two dimension, and jagged array. The default value of array is zero and object set to null. An array start with zero index, mean if array size have 5 then indexing start to zero “0” and end with 4. An array can be any type like int, float, string, object type.

Types of array

  1. Single dimension array
  2. Multi dimensional array
  3. Jagged array

Single dimension array

 We create the single dimension using “new” operator. This contain the elements array[0] to array[ array.size -1 ].

Array declaration-

Syntax .

 Datatype-name[] name=new datatype[ size ];

Example –

 int[] p=new int[6];

Array initialization

  Int[] number=new int[] {1,2,3,4,5};

 Int[] number={1,2,3,4,5};

Int[] number=new int[9];

 Number[0]=1;

Number[1]=2;

Number[2]=3;

Number[3]=4;

.

.

Number[8]=9;

Multidimensional dimension array

 Array can have more than one dimension . for example two, three, for dimension.

 Two dimensional array syntax

  int[,] array = new int[4, 2];

in this example 4 represent the row and 2 is represent to column.

int[,] array2D = new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };

int[,] array2Da = new int[4, 2] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };

string[,] array2Db = new string[3, 2] { { 'one', 'two' }, { 'three', 'four' },

                                        { 'five', 'six' } };

 Three dimensional array syntax

  int[,,] array1 = new int[4, 2, 3];

in this declaration show that, 4 represent to row of 3rd dimensional array of 2X3 two dimensional.

int[,,] array3D = new int[,,] { { { 1, 2, 3 }, { 4, 5, 6 } },{ { 7, 8, 9 }, { 10, 11, 12 } } };

int[,,] array3Da = new int[2, 2, 3] { { { 1, 2, 3 }, { 4, 5, 6 } },{ { 7, 8, 9 }, { 10, 11, 12 } } };

Jagged array

 an array is an array whose elements are array. Other words “array of array” is called jagged array. This array flexible at runtime.

 int[][] jaggedArray = new int[3][];

Before use of jagged array use must initialize the elements.

 jaggedArray[0] = new int[5];

jaggedArray[1] = new int[4];

jaggedArray[2] = new int[2];

jaggedArray[0] = new int[] { 1, 3, 5, 7, 9 };

jaggedArray[1] = new int[] { 0, 2, 4, 6 };

jaggedArray[2] = new int[] { 11, 22 };

Other declaration

int[][] jaggedArray3 =

{

     new int[] { 1, 3, 5, 7, 9 },

     new int[] { 0, 2, 4, 6 },

     new int[] { 11, 22 }

};

It is possible to mix the jagged and multidimensional array

 int[][,] jaggedArray4 = new int[3][,]

{

     new int[,] { {1,3}, {5,7} },

     new int[,] { {0,2}, {4,6}, {8,10} },

     new int[,] { {11,22}, {99,88}, {0,9} }

};

class ArrayTest

{

    static void Main()

    {

        // Declare the array of two elements.

        int[][] arr = new int[2][];

        // Initialize the elements.

        arr[0] = new int[5] { 1, 3, 5, 7, 9 };

        arr[1] = new int[4] { 2, 4, 6, 8 };

        // Display the array elements.

        for (int i = 0; i < arr.Length; i++)

        {

            System.Console.Write('Element({0}): ', i);

            for (int j = 0; j < arr[i].Length; j++)

            {

                System.Console.Write('{0}{1}', arr[i][j], j == (arr[i].Length - 1) ? '' : ' ');

            }

            System.Console.WriteLine();

        }

        // Keep the console window open in debug mode.

        System.Console.WriteLine('Press any key to exit.');

        System.Console.ReadKey();

    }

}



Hi, my self Ravi Vishwakarma. I have completed my studies at SPICBB Varanasi. now I completed MCA with 76% form Veer Bahadur Singh Purvanchal University Jaunpur. SWE @ MindStick | Software Engineer | Web Developer | .Net Developer | Web Developer | Backend Engineer | .NET Core Developer

Leave Comment

Comments

Liked By