Hi.....
Ques : Hello friend now I am working a collection in a .Net. But i have some confusion why we have used Generic collection in a .Net ? please explain and give a example.
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Prabhu RajaPosted Feb 2, 2012, 1:15 PM
Generics gives you the ability to create a generic methods or a generic type by defining a placeholder for method arguments or type definitions, which are specified at the time of invoking the generic method or creating the generic type.
This has been Referred from Articles:
http://www.c-sharpcorner.com/uploadfile/Ashush/generics-in-C-Sharp-part-i/
http://www.c-sharpcorner.com/uploadfile/Ashush/generics-in-C-Sharp-part-ii/
Satyapriya NayakPosted Feb 2, 2012, 11:55 AM
Using generic collections is generally recommended, because you gain the immediate benefit of type safety without having to derive from a base collection type and implement type-specific members. In addition, generic collection types generally perform better than the corresponding nongeneric collection types (and better than types derived from nongeneric base collection types) when the collection elements are value types, because with generics there is no need to box the elements.
Refer
http://msdn.microsoft.com/en-us/library/ms172181%28v=vs.80%29.aspx
http://www.csharp-station.com/Tutorials/Lesson20.aspx
http://www.c-sharpcorner.com/uploadfile/Ashush/generics-in-C-Sharp-part-i/default.aspx
Thanks
VulpesPosted Feb 2, 2012, 11:47 AM
Take List
List
list.Add(3);
int value = list[0]; // value is set to 3
Before generics were introduced in .NET 2.0, one had to use the ArrayList instead. This was weakly typed (everything was stored internally as of type Object) and, worse still, when structs were added they had to be boxed on the heap (a slow process) and then unboxed when they were retrieved. The corresponding code for the ArrayList would have been:
ArrayList aList = new ArrayList();
aList.Add(3); // 3 has to be boxed so it can be stored as an Object
int value = (int)aList[0]; // value is set to 3 which has to be unboxed first using a cast