15-12-2010
Reference Type: In reference types, the data will be storing into heap memory due to that reason we can call it has heap based data types.
In reference type variable contain address of actual data.
Heap: heap is a data structure which can contain collection of elements, each element is representing as node.
Heap is not following any approach called LIFO or FIFO. It is following its own approach called "Random Approach" i.e., we can insert an element as well as we can retrieve elements randomly.
Array: Array is a collection of homogeneous data elements i.e., an array contain same data type elements, whenever we want to represent a collection of same data type elements with a single name then we can go for array concept.
Each element of a array is representing within one index value. First element's index value will be 0 and second element index value will be 1, and the last element index will be (size-1).
Whenever we want to access an element of array we can access by using array name with concern index value.
a[2] => 30
a[4] => 50
C#.Net will support three types of array:
1. One dimensional array
2. Multi dimensional array / 2-dimensional array
3. Jagged array
1. One-dimensional array: Arranging the number of elements in a single row can be called as one-dimensional array.
Synatx: <data type> [] <array name> = new <data type> [size];
[] --> subscription operators.
size --> number of elements
Eg: for one-dimensional integer array:
Void main()
{
int [] a = new int[]{10, 20, 30, 40, 50};
//Printing
for(int i = 0; i <a.length; i++)
Console.Write(a[i] +" ");
Console.ReadLine();
}
Output: 10 20 30 40 50
Eg: String array
Void main()
{
String [] city = new string[] {"Hyderabad", "Delhi", "Pune", "Chennai"}
for(int i = 0; i < city.length; i++)
Console.write(a[i] +"");
Console.ReadLine()
}
2-Dimensional / Multi Dimensional Array: Arraning the number of elements in matrix format can be called as multi-dimensional array.
In multi-dimensional array all the rows should have the same number of elements or columns.
Syntax: <Data type> [,] <array name> = new <data type> [size, size];
Eg: for multi-dimensional array:
Void main()
{
int [,] a = new int [,] {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}};
int i = 0;
int j = 0;
while(i < 3)
{
while(j < 4)
{
console.write(a[i,j] +" ";
j++;
}
Console.WriteLine();
i++;
j = 0;
)
}
OUTPUT: 1 2 3 4
5 6 7 8
9 10 11 12
In reference type variable contain address of actual data.
Heap: heap is a data structure which can contain collection of elements, each element is representing as node.
Heap is not following any approach called LIFO or FIFO. It is following its own approach called "Random Approach" i.e., we can insert an element as well as we can retrieve elements randomly.
Difference Between Value Types & Reference Types:
Array: Array is a collection of homogeneous data elements i.e., an array contain same data type elements, whenever we want to represent a collection of same data type elements with a single name then we can go for array concept.
Each element of a array is representing within one index value. First element's index value will be 0 and second element index value will be 1, and the last element index will be (size-1).
Whenever we want to access an element of array we can access by using array name with concern index value.
a[0] => 10
a[2] => 30
a[4] => 50
C#.Net will support three types of array:
1. One dimensional array
2. Multi dimensional array / 2-dimensional array
3. Jagged array
1. One-dimensional array: Arranging the number of elements in a single row can be called as one-dimensional array.
Synatx: <data type> [] <array name> = new <data type> [size];
[] --> subscription operators.
size --> number of elements
Eg: for one-dimensional integer array:
Void main()
{
int [] a = new int[]{10, 20, 30, 40, 50};
//Printing
for(int i = 0; i <a.length; i++)
Console.Write(a[i] +" ");
Console.ReadLine();
}
Output: 10 20 30 40 50
Eg: String array
Void main()
{
String [] city = new string[] {"Hyderabad", "Delhi", "Pune", "Chennai"}
for(int i = 0; i < city.length; i++)
Console.write(a[i] +"");
Console.ReadLine()
}
2-Dimensional / Multi Dimensional Array: Arraning the number of elements in matrix format can be called as multi-dimensional array.
In multi-dimensional array all the rows should have the same number of elements or columns.
Syntax: <Data type> [,] <array name> = new <data type> [size, size];
Eg: for multi-dimensional array:
Void main()
{
int [,] a = new int [,] {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}};
int i = 0;
int j = 0;
while(i < 3)
{
while(j < 4)
{
console.write(a[i,j] +" ";
j++;
}
Console.WriteLine();
i++;
j = 0;
)
}
OUTPUT: 1 2 3 4
5 6 7 8
9 10 11 12