Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
When we have array then what is the use of generic collection as it supports only one type at the time of declaration as an array does
Posted
Comments
Sergey Alexandrovich Kryukov 18-Nov-11 11:27am    
What do you want to achieve?
--SA

Generic collections are different and have different features arrays do not have. I don't want to list them all and point it out.

Generally, they allow for some operations which can only be achieved if you re-create entire list, such as random add/insert and remove. You can all elements to an array as well but as you choose the length of it in advance, it may require reallocation of an array, which is not good and not transparent if you only need to add one element. Other generic classes provide much faster speed of operations such as search by key. In three classes using key-value pairs, the complexity is O(1) while in arrays this is O(N) (for "Big O Notation", see http://en.wikipedia.org/wiki/Big_O_notation[^].)

For more information, see http://msdn.microsoft.com/en-us/library/system.collections.generic.aspx[^].

For all new development you should not use non-generic collections. They are not formally marked "obsolete" (because there is no urgent need to replace them in legacy code), but for new development they are certainly rendered obsolete in .NET Framework v.2.0 when generics were first introduced. These collection classes allows to work without typecasting to element types, which is very important.

—SA
 
Share this answer
 
v2
An Array stores Objects, which since everything in .NET is derived from Object that means it could store anything. A Generic collection on the other hand stores on the type specified

Object[]arr = new Object[5];
arr[0] = new Foo();
arr[1] = new Bar();
arr[2] = 1;
arr[3] = string.empty;


All of these are valid but you expected a string or an integer in the array that would cause a lot of problems.

List<Foo> list = new List<Foo>();


This of course will only allow objects of type Foo so you know the collection only has Foo objects.

Foo[] arr = new Foo[5];


True this will accomplish almost the same thing. However, a generic list give several other methods that are not available, such as, Add, Remove, Clear that make working with collections much easier.
 
Share this answer
 
Well an array is a specific type of collection. It's fast and efficient but its length (or count) is fixed. It's basically a contiguous block of memory. Sometimes it's desirable to have collections with specific advantages, example a list can grow in size, a stack is optimized for push/pop, a tree provides hierarchial node access, a hashtable allows for key-based fetching etc.
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900