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 them generic?
For example,
Why can't I use string[]
instead of List<string>
?
Let's consider I declare a generic class and it has a generic type parameter X.
T x;
If I provide a method for the class which does
x = x + 1;
What does it mean actually? I don't know what T
is actually going to be and I don't know 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 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
In a nutshell, generics enable types (classes and interfaces) to be parameters when defining classes, interfaces and methods. Much like the more familiar formal parameters used in method declarations, type parameters provide a way for you to re-use the same code with different inputs.
Generics means parameterized types. The idea is to allow type (Integer, String, … etc., and user-defined types) to be a parameter to methods, classes, and interfaces. Using Generics, it is possible to create classes that work with different data types.
Use generic types to maximize code reuse, type safety, and performance. The most common use of generics is to create collection classes. The . NET class library contains several generic collection classes in the System.
Among the benefits of generics are increased code reusability and type safety.
Your question answers itself. Array types are themselves a form of generic typing. Had we put a generic type system in CLR v1.0, I'd be willing to bet that there would be no special array type, there would just be Array<T>
along with List<T>
and so on. Since the first version of the CLR did not have generic types, we put into it the most important and obvious generic type, the array type. And as a result, there is a whole bunch of special-purpose code in there just to handle arrays, the one generic type the CLR supported in v1.0.
Since arrays are essentially a generic type, your question answers itself: the reason for generic types in general is the same reason that motivates the creation of the array type pattern. That is: an array type amplifies the ability of its underlying type in a specific way.
An int represents a number. An int[] represents a collection of numbers; we have amplified the notion of number to the notion of a collection of numbers. A Customer represents a customer. A Customer[] represents the notion of a collection of customers. We have amplified the notion of customer to the notion of collection of customer. The mapping from type T to type T[] represents the abstract notion of generically amplifying an instance of a type to a collection of instances of the type.
That same justification motivates all generic types. The Nullable<T>
type amplifies a type to the notion of "this thing might be an instance of the type". The IComparable<T>
type amplifies a type to the notion of "an instance of this type can be ordered with respect to another instance". And so on. Each generic type is just like the array pattern: it represents the amplification of a type into a new type which provides new operations on that type.
In short: the purpose of the generic type system is to enable you to invent your own type amplifications, and manipulate those amplifications using the type system.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With