What is ment by Generics? How we can define as Generics in Programaticall way?
What is meant by Generics?
Collapse
X
-
Some reading is required here.
Generally they help in type checking. i.e Using generics allows the compiler to help you check the correctness of your code by inferring information from the specified types. They also make your programming easier by not having to put a lot of casts. e.g
if you do
You cannot doCode:ArrayList a = new ArrayList(); a.Add("test");even if you know that the object there is a string.Code:string s = a[0];
You have to use
With generics that is not required, you simply doCode:string s = (string)a[0];
From now on the compiler will be helping you by inserting the casts where required and also stopping you from putting non-strings in that List.Code:List<string> a = new List(); a.Add("test"); string s = a[0]; -
I missed the performance part because I had my Java cap when I typed that reply. (In Java erasure replaces all those generic types with raw types for interoperabilit y with legacy code)Originally posted by stoogots2Good example. Generics also promote flexibility and performance. Remember when you cast (string) or (int), that is a computationally expensive process, something like 4 times slower than just assigning a value.Comment
-
Comment