Arrays: An Array Is A Data Structure That Contains Several Variables of The Same Type. User Defined Reference Type
Arrays: An Array Is A Data Structure That Contains Several Variables of The Same Type. User Defined Reference Type
An array is a data structure that contains several variables of the same type. User defined reference type.
Types of arrays
1. Single Dimension 2. Multi Dimension
Creating an Array
Declaring an array Creating memory locations Putting values into memory locations
Note- The address of first index of array is stored in stack but the array is stored in heap.
Declaration of arrays
type [] arrayName Ex- int [] count; float [] marks; double [] x,y;
Creation of Arrays
arrayName =new type[size]; Ex- count=new int[5]; marks=new float[10];
Initialization
arrayName[index]= value; Ex- marks[0]=98; marks[1]=45; . . . marks[9]=60;
foreach loop
Read only, forward only loop Syntaxforeach(type identifier in expression) { //code here } Ex-foreach (string str in array2) { Console.WriteLine(str); }
Params keyword
static int add(params int[] array) { int total = 0; foreach (int i in array) { total += i; } return total;
Jagged Arrays
Array whose elements are arrays. Syntaxint[][] jaggedArray=new int[3][]; jaggedArray[0]=new int[4]; jaggedArray[1]=new int[5]; jaggedArray[2]=new int[6];