Click here to Skip to main content
15,036,891 members
Please Sign up or sign in to vote.
3.67/5 (5 votes)
See more:
what is difference between generics and collections?
Posted

One thing you need to understand, and which I haven't read in the existing answers yet, is that Collections and Generics have NOTHING to do with each other. They simply complement each other, but Collections can exist without Generics and Generics can exist without Collections.
A collection is simply a collection of whatever. Object, strings, integers, Persons, all of the above...
So what is a 'Generic'? From wikipedia: "In the simplest definition, generic programming is a style of computer programming in which algorithms are written in terms of to-be-specified-later types that are then instantiated when needed for specific types provided as parameters."[^].
That might still not be very simple. But basically a 'Generic' is a method to make something work with ANY type. The pro's to this are, as already mentioned, that you avoid 'boxing' and 'unboxing' and that your classes are type-safe.
For further reading on Generics:
Generics in the .NET Framework[^]
Generics Explained (.NET Framework version 2.0)[^]
.NET Generics in a Nutshell[^]

So why is it so often mentioned with Collections? Well, think about it. Collections are very important in any application and therefore they are very much used. A Collection is exactly that, a collection. But a collection of what? Object? Of course. But what kind of Object? Wouldn't you like to know... In .NET 1.0 we DIDN'T know! Every collection was a collection of Objects and since everything in .NET is an Object a collection could contain just about anything. Since collections never do anything with the objects they store a collection really doesn't have to know anything about the object. So every type is allowed.
With Generics this is a lot easier. We have collections containing only strings, only integers, only your own custom class type. This, of course, makes it type-safe. We'll never try to cast a string to an integer ever again, because we know the collection contains only strings and not integers.
I like the VB syntax for Generics, since they are more readable than C# and actually imply what it does. The syntax is (Of T). So we could have a List(Of String). Clear, isn't it?

That said, Generics can be used on any Class you want to make (except Controls and Attributes and perhaps some other exceptions).
You can create a generic class or method simply by putting <T> begin it ((Of T) in VB).
C#
public class Test<T>
{
    public T SomeFunction() { // Do something. }
}
In this example SomeFunction returns something of type T. What is T? You don't know yet until you actually create an instance of the class.
C#
Test<int> t1 = new Test<int>();
// SomeFunction returns an int.
int i = t1.SomeFunction();

Test<Person> t2 = new Test<Person>();
// SomeFunction returns a Person.
string name = t2.SomeFunction().Name;
In the example above the problem lies of course in the implementation of SomeMethod because you don't know what T is and how you should return it. In any case, I hope the difference is clear and that you understand Collections and Generics are unrelated, just really complimentary.

C#
// Pre-Generics .NET.
// ArrayList can only hold Objects and ANYTHING .NET is an Object.
ArrayList arrList = new ArrayList();
arrList.Add(1);
arrList.Add("Hello");
arrList.Add(new Person());

Object o = arrList(1); // What is it? What can we do with it?

// Generic example.
List<string> list = new List<string>();
list.Add("Hello");
list.Add("Goodbye");
list.Add(new Person()); // This is not allowed, design-time error!

// You (and the compiler) know this is possible since list(index) ALWAYS returns a string.
if (list(2).Contains("some string"))
{
   // ...
}


Good luck!

<Shameless self-promotion>
P.S. If you are interested, I wrote an article on custom (Generic) Collections.
Having fun with custom collections![^]
</Shameless self-promotion>
   
v2
Comments
Santosh K. Tripathi 5-Jun-15 23:08pm
   
Thanks for clearing concept
Briefly, the basic difference between generic and non-generic collections:

- Non-Generic collections - These are the collections that can hold elements of different data types. It holds all elements as object type.
So it includes overhead of type conversions.

- Generic collections - These are the collections that can hold data of same type and we can decide what type of data that collections can hold.

Some advantages of generic collections - Type Safe, Secure, reduced overhead of type conversions.



Regards
   
we have two type of collection:
Non-Generic collection which collection can hold the different type of data.
Generic collection which collection can hold the same type of data.
and the deference between collections and generics are:
Collections are enumerable data structures that can be assessed using indexes or keys.Using generic collection classes provides increased type-safety and in some cases can provide better performance, especially when storing value types. For more information see:This
   
Generics is a programming tool to make class-independent tools, that are translated at compile time to class-specific ones. Collections is a set of tools that implement collections, like list and so on. In C# 3 and above you have generic collections, that are template based - thus you have generic collections. So, collections in c# are a small subset of generics.
Some theroy: http://en.wikipedia.org/wiki/Generic_programming[^]
   
Generics can also be used when we want to get rid of Boxing or unboxing operation..
   
Non - generic and generic collection?
Generic collections - These are the collections that can hold data of same type and we can decide while initializing what type of data that collections can hold.
Advantages - Type Safe, Secure, reduced overhead of implicit and explicit conversions.

various,
Non generic collections hold elements of different datatypes, it hold all elements as object type.
so it includes overhead of implicit and explicit conversions.
There can be runtime errors - See more at: http://www.developersfaq.com/question/QST-39/difference-between-generic-collection-and-simple-collection-in-c#.Ud6wpPkwd5Q
   
Hi.
With help of Generics you can avoid boxing and unboxing problems.
Generics involves type-safety.
For more details, you can read this Generics

Collection Classes have the following properties:
- Collection classes are defined as part of the System.Collections or System.Collections.Generic namespace.
- Most collection classes derive from the interfaces ICollection, IComparer, IEnumerable, IList, IDictionary, and IDictionaryEnumerator and their generic equivalents.

But for you to note, in NET exists terms of Generic collection.
Generic collection provide increased type-safety and in some cases can provide better performance, especially when they store value types.
   
   
v2
The term "generics" means here two things
1. generics in the C# language
2. the arbitrary but meaningful namespace name System.Collections.Generics

The first allows for type safe universally usable types and methods.
The second is a set of collections based on the above mentioned features.

This is very much simplified, but maybe this is what you were asking for,

Cheers
Andi
   

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