The concept of 'Generics', which is included in .NET Framework version 2.0, and which can be considered very close to Templates in C++.
Version 2.0 of the .NET Framework introduces a new namespace viz.
In the earlier versions, before .NET 2.0, generalization was accomplished by casting types to and from the universal base type
System.Collections.Generic, which contains the classes that support this concept, like the List, Queue, Stack, LinkedList and many more which you can use effectively in your programs.Advantages of Generics
In the earlier versions, before .NET 2.0, generalization was accomplished by casting types to and from the universal base type
System.Object. Generics provide a solution to this limitation in the common language runtime and the C# language. This limitation can be demonstrated with the help of the ArrayList collection class from the .NET Framework base class library. ArrayList is a highly convenient collection class that can be used without any modifications to store any reference or value type.// The .NET Framework 1.1 way of creating a list
ArrayList list1 = new ArrayList(); list1.Add(3);
list1.Add(105);
//...
ArrayList list2 = new ArrayList(); list2.Add("First item.");
list2.Add("Second item");
//...But this convenience comes at a cost. Any reference or value type that is added to an
ArrayList is implicitly typecast toSystem.Object. If the items are value types, they must be boxed when added to the list, and unboxed when they are retrieved. The casting, boxing and unboxing operations degrade performance; the effect of boxing and unboxing can be quite significant in scenarios where you must iterate through large collections.So, what we need is, flexibility of the
ArrayList but it should be more efficient and should provide some ways of type checking by the compiler, without sacrificing on the reusability of that class with different data types. An ArrayList with a type parameter? That is precisely what generics provide. Generics would eliminate the need for all items to be type cast to Object and would also enable the compiler to do some type checking.In the generic
List<T> collection, in System.Collections.Generic namespace, the same operation of adding items to the collection looks like this:// The .NET Framework 2.0 way of creating a list" List(int) list1 = new List(int)(); " List list1 = new List();
list1.Add(3); // No boxing, no casting
list1.Add("First item"); // Compile-time error For the client code, the only added syntax with
List<T> compared to ArrayList is the type argument in the declaration and in instantiation. In return for this slightly greater coding complexity, you can create a list that is not only safer than ArrayList, but also significantly faster, especially when the list items are value types.There is no need to write code to test for the correct data type, because it is enforced at compile time. The need for type casting and the possibility of run-time errors are reduced.
Also Generics got rid of disadvantage of array list by avoiding the type casting.
0 comments:
Post a Comment