Arrays in C#

By
An Array is a kind of variable which can hold multiple variable for same data type.Whenever we create an array then it is automatically inherit from System.Array class.
This class defines a number of methods and properties that can be used to manipulate array more efficiently,which are given below:-
Method/Property                                                  Meaning            
Set value()                                            Sets the value for a given index in the array
Reverse()                                              Reverse the contents of a one -dimension array
sort()                                                      Sorts the elements in a one -dimensional array  
Get Value()                                          Get the value for a given index in the array
Get Length()                                       Gives the number of elements in a given dimension of the array
Clear()                                                   Sets a range of elements to empty values
Copy To()                                            Copies elements from the source array to the destination array
Length()                                                Gives the Length of an array


Note:-Array List Class i have already discussed in Collections.So there is no need to explain again.
Creation of an array involves three steps.
  1. Declaring the array (Declaration of Arrays).
  2. Creating memory locations (Creation of Arrays).
  3. Initialization of Arrays.
     1 . )  Declaration of Arrays:
Arrays are declared in C# as follows:-
Syntax:
type [ ] arrayname ; 
Ex.
int [ ] num;         //declaration int array reference
float [ ] price ;  // declaration float array refernce
Note:- We do not enter the size of the arrays in the declaration.
     2 . ) Creation of Arrays:
We need to create it in the memory after declaration of an array.In C# ,we use a 'new' operator for creating an Array. 
Syntax:
arrayname = new type [ size];
Ex.
num = new int [ 10];      //Create a 10 element int array
price = new float [20 ]; //create a 20 element float array.
Note:- we can combine both steps (Declaration and creation of an array) into one.
Ex.
int [ ] num = new int [ 10];
float [ ] price = new int [20];
     3 . ) Initialization of arrays:-
To put the values in arrays is known as initialization.There are some different Example to initialization  values in the arrays.
Syntax:
arrayname[ subscript] = value;
Ex 1: 
num [ 0] = 20 ;
num [ 1 ] = 30 ;
.
.
num [n ] = n ;
Note:- In C# , An  Array start  with a subscript of 0 (zero) and ends with a value less than the size of the array.
  • Automatic initialization of the array at creation time.
             Syntax:-
                  type [ ] arrayname  = {list of values with commas } ;
             Ex. 2
                     int [ ] num = { 20,40,50,30 };
  • We can  easily combine all three steps (Declaration,creation,initialization) in one.
                    Syntax:
                        int [ ] num = new int [ size] {list of values with commas };
                  Ex. 3
                              int [ ] num = new int [ 10] {20,30,50,40 };
Note:-
Every element in a array is automatically set to a default value.For example if we have an array of numeric type ,each element is set to number 0(zero).
Types of Arrays
 There are two types of Arrays .
  1. Single dimensional arrays
  2. Multi dimensional arrays
     1. )  Single dimensional arrays:-
A list of items that can be given one variable name using one subscript,such a variable is called a single dimensional array. It is also called linear array.

Ex.

using System;
namespace Single_dimensional
{
    class Program
    {
        static void Main(string[] args)
        {
            //declaration,creation and initialization in one step.
            string[] str = new string[4]{"Ram","Mohan","Ajay","Sanjay"}; 
            for (int i = 0; i < 4; i++)
            {
                Console.WriteLine(str[i]);
            }
            Console.ReadLine();
        }
    }
}
Output:

     2. ) Multi dimensional array:-
An array variable that can store the table values, is known as multi dimensional array.It can categories  into two types.
  • Rectangle array
  • Jagged array
Rectangle array:- 
An Array , each row has same size of column, is known as Rectangle array.
Ex.

using System;
namespace arrays
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Enter the Row size");
            int row = int.Parse(Console.ReadLine());
            Console.WriteLine("Enter the Column size size");
            int col = int.Parse(Console.ReadLine());
            int [,] str = new int [row, col];
            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < 2; j++)
                {
                    Console.WriteLine("Enter matrix value ({0},{1})",i,j);
                    str[i, j] = int.Parse(Console.ReadLine());
                }
            }
            Console.WriteLine("\n");
            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < 2; j++)
                {
                    Console.WriteLine("Rectangle arrays values({0},{1}) ",i,j);
                    Console.WriteLine(str[i,j]); 
                }
            }
            Console.ReadLine();   
        }
    }
}
Output:

     Jagged Array:-
Jagged array is an array of array,In which each row has different size of column.


Ex.

using System;
namespace jagged_array
{
    class Program
    {
        static void Main(string[] args)
        {
            int[][] jarr = new int[3][];
            jarr[0] = new int[1];
            jarr[1] = new int[2];
            jarr[2] = new int[3];
            jarr[0][0]=34;
            jarr[1][0]=12;
            jarr[1][1]=38;
            jarr[2][0]=65;
            jarr[2][1]=50;
            jarr[2][2]=43;
            for(int i=0;i<1;i++)
            {
            Console.WriteLine("jagged array 0 elements:[0][{0}]={1}",i,jarr[0][i]);
            }
            for (int i =0; i < 2; i++)
            {
          Console.WriteLine("jagged array 1 elements:[1][{0}]={1}", i, jarr[1][i]);
            }
            for (int i = 0; i < 3; i++)
            {
          Console.WriteLine("jagged array 2 elements:[2][{0}]={1}", i, jarr[2][i]);
            }
            Console.ReadLine();
        }
    }
}
Output:

For More:-

  1. File handling
  2. Collections
  3. Reflection
  4. webform controls
  5. Check constraints

I hope this is helpful for you.
To Get the Latest  Free Updates Subscribe
Click below for download whole application
                        Download

1 comment:

Powered by Blogger.