'Why' and 'Where' Generics is actually used?

Posted by NLV on Stack Overflow See other posts from Stack Overflow or by NLV
Published on 2010-03-19T06:42:24Z Indexed on 2010/03/19 6:51 UTC
Read the original article Hit count: 274

Filed under:
|

Hi all

I know that generics are used to achieve type safety and i frequently read that they are largely used in custom collections. But why actually do we need to have it generic?

For example,

Why cant i use string[] instead of List<string>

Lets consider i declare generic class and it has a property X

T x;

If i provide a method for the class which does

x = x + 1;

what does it mean actually? I dono what T is actually going to be and i dono what x = x + 1 going to perform actually?

If i'm not able to do my own manipulations in my methods how the generics are going to be help me anyway?

I've already studied lot of bookish answers. It would be much appreciated if any one can provide some clear insights in this.

Regards NLV

© Stack Overflow or respective owner

Related posts about generics

Related posts about c#