Array in C#

Array

It is a set of similar type of value that are stored in a sequential order either as rows or columns and we access value from array using its index position.

An array index start with zero (0),that means first item of an array will be stored at zeroth 0th position of last item of an array will be total number of item minus 1.

In c# array can be definded as fixed length and dynamic length.

Where fixed length of array can be stored predefined no of items.

While size of dynamic arrary increase as you add new item to the array.

In c# we have 3 different types of array:

  1. Single dimentional array
  2. Two dimentional array
  3. Jagged array

1. one dimentional array

This array stores the values in the form of row.

Syntax

<Type> []<name>=new <type>[size];

Eg

Int [ ] arr=new int [4];
------- Or -------
Int [] arr;
Arr=new [5];
------- or ------
Int [] arr={array element};

Note - An array that initializer either with use of a new operator or assignment of values.

Eg

using System;

using System.Collections.Generic;

using System.Linq;

//using System.Text;

using System.Threading.Tasks;

 

namespace ArrayDemo

{

    class onedimentional

    {

        //decleration of array

        int[] arr = new int[5];

      

       

        public void print()

        {

            arr[0] = 10; arr[1] = 20; arr[2] = 30; arr[3] = 40; arr[4] = 50;

            Console.WriteLine("---------------using the for  loop----------");

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

            {

                Console.WriteLine(arr[i] + " ");

            }

            Console.WriteLine("---------------using the foreach loop----------");

 

            //foreach loop

            foreach (int i in arr)

            {

                Console.WriteLine(i + " ");

            }

 

        }

          static void Main(string[] args)

        {

            onedimentional obj = new onedimentional();

            obj.print();

            Console.ReadKey();

 

        }

    }

}

Difference between th for loop and foreach loop array values

  1. Incase of for loop the loop variable referes to index of array where as incase of foreach loop the loop variable referes to values of the array.
  2. Incase of for loop the loop variable always will be int only.
  3. Incase of foreach only the datatype of loop variable will be same of the datatyeps of values presene on the array
  4. Incase of for loop we can use it for both accessing and assigning values to the array.

Whenas foreach loop can be used only for acccesing the values but not assigning the values.

Array Class

It is predefinded class under the libraries that provides you a set f member which can be applied in an array for manupulating its values.

Array Classes

  1. sort (<array>)
  2. reverse (<array>)
  3. copy (<src,dest,n>)
  4. getlength(int)
  5. length

Eg

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

 

namespace ArrayDemo

{

    class ArrayClasses

    {

            int[] arr = { 12, 45, 60, 10, 63, 98, 23, 52, 63 };           

            public void sortmethod()

            {

                Console.WriteLine("Using for sort method");

                Array.Sort(arr);

                foreach (int i in arr)

                {

                    Console.Write(i);

                    Console.WriteLine();

                }

            }

            public void reversemethod()

            { 

                Console.WriteLine("Using for reverse  method");

                Array.Reverse(arr);

                foreach (int i in arr)

                {

                    Console.Write(i);

                    Console.WriteLine();

                }

            }

            public void Copymethod()

            { 

                Console.WriteLine("Using for Copy  method");

                int [] brr=new int [10];

                Array.Copy(arr, brr, 5);

            

                foreach (int i in brr)

                {

                    Console.Write(i);                   

                    Console.WriteLine();

                }

            } 

            static void Main()

            {

                ArrayClasses obj = new ArrayClasses();

                obj.sortmethod();

                obj.reversemethod();

                obj.Copymethod();

                Console.ReadKey();

            }

 

    }

}

your feedback and suggestion is always welcome for me. 

Ebook Download
View all
Learn
View all