Jitesh Byahut 4/12/2014 3718

Array In C#

Array is collections of similar data type. It's a reference data type in C#. First index of array is known as Lower Bound of array. Last index of array is known as Upper Bound of array.

Declaration Syntax:

data-type[] array-name = new data-type[];

Array started from 0 index, suppose an array contains n elements then, index started from 0 to n-1

The new operator is used to create the array and initialize the array elements to their default values.

        // Array declaration

        int[] array1 = new int[5];

 

        array1[0] = 5;

        array1[1] = 25;

        array1[2] = 10;

        array1[3] = 15;

        array1[4] = 20;

Types of array


1. Single Dimensional Array

2. Multi Dimensional Array

3. Jagged Array

Single Dimensional Array

Array which has only one row is known as Single Dimensional array.

        // Single Dimensional Array Declaration  Types

 

        // Declaration Type 1

        int[] array1 = new int[5];

 

        array1[0] = 5;

        array1[1] = 25;

        array1[2] = 10;

        array1[3] = 15;

        array1[4] = 20;

 

        // Declaration Type 2

        int[] array2 = new int[5] { 5, 25, 10, 15, 20};

 

        // Declaration Type 3

        // You can omit the size of the array, in this case array internally

        //  use their size as total number of elements at declaration time

        int[] array3 = new int[] { 5, 25, 10, 15, 20};

 

        // Declaration Type 4

        // You can also omit the new operator if an initializer is provided, like this:

        int[] array4 = { 5, 25, 10, 15, 20 };

 

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

        {

            Console.WriteLine(array[i]);

        }

 

// Output

// 5 25 10 15 20

Multi Dimensional Array

Array which has more than one row and one column is known as Multi Dimensional array

        // Multi-Dimensional Array Declaration

 

        // data-type[,] array-name = new data-type[row,column]

 

        // Declaration Type 1

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

        array1[0, 0] = 1;

        array1[0, 1] = 2;

        array1[0, 2] = 3;

        array1[1, 0] = 4;

        array1[2, 1] = 5;

        array1[3, 2] = 6;

 

        // Declaration Type 2

        int[,] array2 = new int[2, 3] { { 1, 2, 3 }, { 4, 5, 6 } };

 

        

        // Declaration Type 3

        // You can omit the size of the array, in this case array internally

        // use their size as total number of elements at declaration time

        int[,] array3 = new int[,] { { 1, 2, 3 }, { 4, 5, 6 } };

 

        // Declaration Type 4

        //You can also omit the new operator if an initializer is provided

        int[,] array4 = { { 1, 2, 3 }, { 4, 5, 6 } };

 

 

        // Access multi-dimnesional array.

        for (int row = 0; row < 2; row++)

        {

            for (int col = 0; col < 3; col++)

            {

                // Print individual values

                Console.Write(array1[row, col] + " ");

            }

            // change next line for each row

            Console.WriteLine();           

        }

 

        // Output

        //    1  2  3

        //    4  5  6

Jagged Array

A Jagged array is a type of array whose elements are arrays. It is also known as Array of Arrays. The elements of Jagged array can be different size and dimensions.

            // Jagged Dimensional

 

            // data-type[][] array-name = new data-type[row size][]

 

            // Declaration Type 1

            int[][] array1 = new int[4][];

            array1[0] = new int[1] { 1 };

            array1[1] = new int[2] { 5, 10 };

            array1[2] = new int[3] { 2, 4, 6};

            array1[3] = new int[4] { 4, 8, 12, 16};

 

 

            // Declaration Type 2

            int[][] array2 = new int[4][] { new int[1] { 1 }, new int[2] { 5, 10 }, 

                                                                               new int[3] { 2, 4, 6 }, new int[4] { 4, 8, 12, 16 } };

 

            // Declaration Type 3

            // You can omit the size of the array, in this case array internally

            // use their size as total number of elements at declaration time

            int[][] array3 = new int[][] { new int[1] { 1 }, new int[2] { 5, 10 }, 

                                                                               new int[3] { 2, 4, 6 }, new int[4] { 4, 8, 12, 16 } };

 

           

 

            // Declaration Type 4

            //You can also omit the new operator if an initializer is provided

            int[][] array4 = { new int[1] { 1 }, new int[2] { 5, 10 }, 

                                                                                    new int[3] { 2, 4, 6 }, new int[4] { 4, 8, 12, 16 } };

 

            // Access Jagged array elements

            for (int row = 0; row < array1.Length; row++)

            {

                for (int col = 0; col < array1[row].Length; col++)

                {

                    Console.Write(array1[row][col] + " ");

                }

                Console.WriteLine();

            }

 

            // Output

            // 1

            // 5 10

            // 2 4 6

            // 4 8 12 16

Length

Array's Length property use to get total number of elements in array.

            // Declaration of a single dimensional array

            int[] array = new int[10] { 5, 25, 10, 15, 20, 45, 30, 35, 40, 50 };

 

            // Find number of elements in an array

            int len = array.Length;

 

            Console.WriteLine(len);

 

            // Output

            // 10

 

            // Declaration of a multi dimensional array

            int[,] array = new int[2, 3] { { 1, 2, 3 }, { 4, 5, 6 } };

 

            // Find number of elements in an array

            int len = array.Length;

 

            Console.WriteLine(len);

 

            // Output

            // 6

Rank

Get rank of an array.

            // Declaration of a single dimensional array

            int[] array = new int[10] { 5, 25, 10, 15, 20, 45, 30, 35, 40, 50 };

 

            int rank = array.Rank;

            Console.WriteLine(rank);

 

            // Output

            // 1

 

            // Declaration of a multi dimensional array

            int[,] array = new int[2, 3] { { 1, 2, 3 }, { 4, 5, 6 } };

 

            int rank = array.Rank;

            Console.WriteLine(rank);

 

            // Output

            // 2

CopyTo

Copy one array elements into another array from specific index.

            int[] array1 = new int[5] { 5, 25, 10, 15, 20 };

 

            int[] array2 = new int[10] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

 

 

            // Copy array1 elements into array2 from index start from 0

            array1.CopyTo(array2, 0);

 

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

            {

                Console.WriteLine(array2[i]);

            }

            // Output

            // 5 25 10 15 20 5 6 7 8 9

 

 

 

            // Copy array1 elements into array2 from index start from 4

            array1.CopyTo(array2, 4);

 

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

            {

                Console.WriteLine(array2[i]);

            }

 

            // Output

            // 0 1 2 3 5 25 10 15 20 9

 

            //// In this condition only 3 elements copied in array2 and

            //// remaining elements are out of bound so,

            //// it will throw an exception - Destination array was not long enough

            array1.CopyTo(array2, 8);

Reverse

Array's Reverse method reverse sequence of all elements in the array.

Its simply reverse index position of elements, for example first index element shifted on last index.

Its overloaded method allow to reverse elements between a particular range in the array.

            // Declare an array of type int

            int[] array1 = new int[5] { 5, 25, 10, 15, 20 };

            // Reverse the elements of array

            System.Array.Reverse(array1);

 

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

            {

                Console.WriteLine(array1[i]);

            }

            // Output

            // 20  15  10  25  5

 

            // Declare an array of type string

            string[] array2 = new string[5] { "Delhi", "Mumbai", "Calcutta", "Chennai", "Bangalore"};

            // Reverse the elements of array

            System.Array.Reverse(array2);

 

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

            {

                Console.WriteLine(array2[i]);

            }

            // Output

            // Bangalore  Chennai  Calcutta  Mumbai  Delhi

SetValue

Set a specific index position element to a value in the already declared array.

            int[] array = new int[5] { 5, 25, 10, 15, 20 };

 

            // Replace the specific index value

            // Replace the 0 index element i.e 5 is replace by 40

            array.SetValue(40, 0);

 

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

            {

                Console.WriteLine(array[i]);

            }

 

            // Output

            // 40 25 10 15 20

System.Array.Sort

Sort method sort the elements according to their data-type, if data-type is string the it sort according to alphabet and if data-type is int then it sort according to ascending order which is default sorting order.

            // Declare an array of type int

            int[] array1 = new int[10] { 5, 25, 10, 15, 20, 45, 30, 35, 40, 50 };

 

            // Sort the elements of an array by ascending order

            System.Array.Sort(array1);

 

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

            {

                Console.WriteLine(array1[i]);

            }

           

            // Output

            // 5  10  15 20  25  30  35  40  45  50

 

            // Declare an array of type string

            string[] array2 = new string[5] { "Delhi", "Mumbai", "Calcutta", "Chennai", "Bangalore"};

 

            // Reverse the elements of an array according to alphabets

            System.Array.Sort(array2);

 

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

            {

                Console.WriteLine(array2[i]);

            }

 

            // Sorting by descending order

            int[] array = new int[5] { 5, 25, 10, 15, 20 };            

 

            // First Sort the array

            System.Array.Sort(array);

 

            //After then Reverse the array

            System.Array.Reverse(array);

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

            {

                Console.WriteLine(array[i]);

            }

 

            // Output

            // 25 20 15 10 5

 

System.Array.Resize

Resize the already declared array size.

Array must be a one-dimensional array.

If array is null, this method creates a new array with the specified size.

If newSize is greater than the Length of the old array, a new array is allocated and all the elements are copied from the old array to the new one. If newSize is less than the Length of the old array, a new array is allocated and elements are copied from the old array to the new one until the new one is filled; the rest of the elements in the old array are ignored. If newSize is equal to the Length of the old array, this method does nothing.

            int[] array = new int[5] { 5, 25, 10, 15, 20 };

 

            // Resize the the array size

            System.Array.Resize(ref array, 10);

 

            array[5] = 30;

            array[6] = 35;

            array[7] = 40;

            array[8] = 45;

            array[9] = 50;

 

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

            {

                Console.WriteLine(array[i]);

            }

 

            // Output

            // 5 25 10 15 20 30 35 40 45 50

System.Array.BinarySearch

            // Binary Search

            int[] array = new int[5] { 5, 25, 10, 15, 20 };

 

           // Search any element from the array

           int a= System.Array.BinarySearch(array, 10);

           // Output 

           // 2

 

           // Output is negative if element is not exists in the array

           int b = System.Array.BinarySearch(array, 55);

           // Output

           // -6

System.Array.Clear

Clear method set the default value of each element in the array.

This method only clears the values of the elements; it does not delete the elements themselves. An array has a fixed size; therefore, elements cannot be added or removed.

            int[] array = new int[5] { 5, 25, 10, 15, 20 };

 

            // Clear all elements from an array and set to default values

            System.Array.Clear(array, 0, 5);

 

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

            {

                Console.WriteLine(array[i]);

            }

 

            // Output

            // 0 0 0 0 0

System.Array.Copy

Copy one array elements into another array.

            int[] array1 = new int[5] { 5, 25, 10, 15, 20 };

 

            int[] array2 = new int[10] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

 

            // Copy one array into another array of same type

            // pass int value for how many elemnets are copy

            System.Array.Copy(array1, array2, 2);

 

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

            {

                Console.WriteLine(array2[i]);

            }

 

            // Output

            // 5 25 2 3 4 5 6 7 8 9

            // Only 2 elements are copied into array2

Clone

Clone method create clone of an array means it creates a shallow copy of the array.

Clone create new array whose data-type is same as old array

A shallow copy of an Array copies only the elements of the Array, and they are reference to the same object as the reference to elements in the original array.

A deep copy of an Array copies the elements and everything directly or indirectly referenced by the elements.

            // Create Clone of array1 and store in new array object array2

            int[] array1 = new int[5] { 5, 25, 10, 15, 20 };

 

            int[] array2 = (int[])array1.Clone();

 

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

            {

                Console.WriteLine(array2[i]);

            }

 

            // Output

            // 5 25 10 15 20


Please give your feedback for improving this page