Techno

Array |Types of Arrays - C#

An array is a data structure that contains a no of elements of the same type. In C# all array is declared by defining the type f the elements inside the array followed by empty rackets and a variable name.

Syntax (Single Dimensional Array)


data_type[] array_name=new data_type[size];

Example:
•	int[] my_array=new int[8];
•	int[] my_array={1,2,3,4};
•	int[] my_array=new int [4] {1,2,3,4};
•	for(int i=0;i<4;i++)
{
Console.WriteLine(my_array[i]);
}

  for each loop:

It is used when array size is not known and to print each element of the array when we want the input from the user.

Syntax


foreach(int val in my_array)
{
Console.WriteLine(val);
}
Example (Two Dimensional Arrays)

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


Jagged array It is a special kind array. In each row, there is no different column only possible in 2-D.

Example 
int[][] my_array=new int[3][ ];
my_array [0]=new int {3,5};
my_array [1]=new int {3,5,6,3};
my_array [2]=new int {3,5,9};
Note: - In this array define an only number of rows not the number of columns.
Checked operator If we mark a block of code as checked the CLR will enforce overflow checking once throw an exception if overflow occurs.
Unchecked operator
It is used to suppress overflow checking, in this case, no execution will be raised but we will lose the data.
Example byte b=255; checked { b++; //message will display when the value of b goes out of range. } If unchecked no message will come and data will be lost.