Difference Between Array - Generic - Collections in Csharp
Array :An Array is Fixed Size Data Type . For Storing data in Array you need to define the size of array and Also Type of Array ( int, string , float etc )
Depending on Memory Allocation Data Types are of Two Types -
- Value Type
- Reference Type
See Difference Between Value Type and Reference Type in Link Below -
http://geeksprogrammings.blogspot.in/2015/09/value-types-vs-reference-types-dotnet.html
Array is a Value Type that needs to define its size in order to allocate space to it in memory . This is little problem with array as sometimes we need Dynamic Size to store large number of values
Collections :
Collections are Defined in System.Collections Namespace . Collections Are Variable Sized . We Can add / remove items from collection with any restriction of defining size and type of data
Example :- Arraylist are Collections .
Logically Collections are Referency Types . Now Question is Why Collections are Referencec Types ?
Collections are of Variables Sized So Whenever Some Type is Variable Sized It needs to be stored in head instead of Memory Stack give to application . and poiter to that heap is set in the top of stack . it is stored in heap because main memory is valuable and we don't exactly know that How much size variable sized Collection will take so it is allocated disk space as heap instead of storing it directly in main memory Pointer to heap location is set to top of stack so whenever we require it we can use that pointer to get / set that value .
See Difference Between Value Type and Reference Type ( Stack & Heap ) in Link Below -
http://geeksprogrammings.blogspot.in/2015/09/value-types-vs-reference-types-dotnet.html
Example of Collections -
ArrayList arrList = new ArrayList();
arrList.Add(007);
arrList.Add("Heemanshu Bhalla");
arrList.Add(DateTime.Now);
If we want to loop that arrList then we can do it as below -
foreach (object o in arrList)
{
}
But there is one problem we always need to parse each element in collection using object as we don't know the type of data that it contains and that could create problem on runtime
Problem With Collections & Evolution of Generics
Generic Types Solves This problem check Generic Types Below -Generic Types :
Generic Types Contained in System.Collections.Generic Namespace . It removes the problem of Unknown Data types exist in Collections
Generic List ( List <T> ) , Here T means datatype that could be Int , String , DateTime etc . So We can define the type of data that we want to store and we can also parse the data simple as type is already known . SO it is Type Safe . If you Create Int Generic List or Generic Type and try to store some different type then it will give compilation Error and does not allow you to store data of Another Type that is not allowed .
Example of Generic List below -
List<string> lstString = new List<string>();
lstString.Add("Heemanshu Bhalla");
lstString.Add("I am A Geek");
List<int> lstInt = new List<int>();
lstInt.Add(007);
lstInt.Add(786);