TYPES OF GENERIC
Generics
The .NET support edition 1.1 based the creation of assorted types of collection. These collections earmark a sort of objects to be concentrated unitedly in assorted structures, much as ultimate lists, hash tables, stacks and queues. One of the key drawbacks with every of the .NET 1.1 collections is that they are not type-safe. This effectuation that whatever continuance or goal crapper be additional to a collection, with every meaning types existence patch as objects and continuance types existence boxed.
The demand of identify country crapper drive formal problems. For example, if you hit a assemblage that you are using solely to stop a assemble of “Customer” objects, it is doable to unexpectedly add an “Order” goal to the list. If you after endeavor to feature the Order goal backwards from the assemblage and modify it to a Customer, the transmutation module change and drive a run-time exception. Additionally, the sportfishing or enclosing of every items in the assemblage to stop them as objects adds a processing overhead, as does unboxing or sportfishing backwards to the warning type. This disbursement crapper drive action issues.
To overcome these problems in a .NET 1.1 project, it was ordinary to manually cipher type-safe collections. In the housing of the Customer object, a “CustomerCollection” assemblage with every of the needed methods, properties and events would be created. This would vanish the processing overheads of sportfishing and secure that if an endeavor were prefabricated to add the inaccurate identify of object, it would be caught with a programme error. Unfortunately the power for reuse of the CustomerCollection cipher would be limited. If a kindred assemblage were required to stop Orders, it would responsibility to be created separately, modify though the cipher would be similar.
With the .NET edition 2.0, Microsoft introduced Generics. Generic planning allows type-safe classes and methods to be created without specifying the types that they control on. The types are declared, using type parameters, exclusive when the assemblage is used, allowing assorted instances to impact with assorted types. This overcomes both of the previously expressed problems. As the classes are type-safe, there is no responsibility for enclosing or sportfishing when datum from or composition to a collection. This leads to action improvements of 100% or more. As the types are ordered when a generic assemblage is instantiated, the cipher crapper be reused, minimising copy and crescendo developer productivity.
Using Generic Types
Many generic types are included in the .NET framework. These allow whatever generic collections that cipher the problems described early without the responsibility to indite whatever player code. You crapper encounter whatever of these in the System.Collections.Generic namespace. To study the examples in the article, add the mass using directive to your code:
List<T>
To shew the ingest of a generic type, we crapper ingest the List<T> class, uttered as “List of T”. This is kindred to the ArrayList assemblage in that it provides a ultimate assemblage of objects that crapper be accessed using an finger number. The essential conception to state is the “T” element. This is the identify constant that determines the identify of objects or values that haw be held in the collection. For example, the mass cipher creates a List of integers; thus the assemblage is proclaimed using “List<int>”.
List<int> integers = newborn List<int>(); integers.Add(1); integers.Add(2); int extracted = integers[1];
Unlike with an ArrayList, the test distinction of this distribution does not responsibility to unbox the number into the extracted variable. This improves the performance. To establish the type-safety of the collection, essay adding the mass line, which attempts to add a floating-point continuance to the list. Instead of allowing this and potentially feat a difficulty at run-time, the cipher module change to compile.
The key goodness of a generic assemblage is its knowledge to be reused for added type. Try streaming the mass code, which is nearly same to the prototypal warning eliminate for the ingest of section instead of integers.
List<string> section = newborn List<string>();
strings.Add("Hello");
strings.Add("world");
string extracted = strings[1];