Generics
Generics are classes or methods that work uniformly on values of different types Generics also called as Parametric Polymorphism, it is similar to the concept of Templates in c++, which is lacked in .Net frame work1.1 and is new feature of C# 2.0. Generics allow you to realize type safety at compile time. They allow you to create a data structure without committing to a specific data type. When the data structure is used. Even though C# 2.0 Generics are similar to Templates in c++, but we can’t compare both because generics have some additional benefits and it solved the problems like Developer Confusion.
The System.Collections.Generics namespace contains the generics collections in .NET 2.0. Various collections/container classes have been "parameterized."
For example, if one wanted to create a list using generics, a possible declaration would be to say List, where T represented the type. When instantiated, one could create List or List. The list is then treated as a list of whichever type is specified.
Prior to Generics all dotnet developers allowed making their implementations type independent by the use of objects. Since every data type inherits from object all the .Net types can be upcasted to type objects. This approach has two main disadvantages
1. When using objects for the basic types, there are boxing operations happening as below. Suppose we have a statement like below:
int x = 5;
object y = x;
Its corresponding code will look like below:
IL_0025: box [mscorlib]System.Int32
Similarly when down casting the object to integer, unboxing happens. Both boxing and unboxing are performance sensitive operations in fact. Thereby generics enable building applications with better performance.
2. Compile time error checking.
Since objects can accept values of any types, compile time errors do not occur if the value of wrong type is pushed into object. However this may be caught in the run time which is not something desirable for developers.
Generics solves the above problems
1. Generics can make your Code type safe and prompt you of errors during compile time itself. 2. With Generics we do not end up Boxing and un boxing hence there is a great performance.
Advantages of Generics
1. Generics solve the problem of type safety 2. Code Reusability: if we write a Generic Class or method once we can use that through out our program. hence it is code reuse 3. Performance By using Generics performance increases ,this is because we are not using the concept of Boxing 4. Clarity: with generics, you're never faced with the cryptic compiler errors that plague C++ template users. Constraints are a feature in generics that have the effect of making incompatible expansions of generic code impossible. 5 Generic methods can often be called without employing any special syntax by using a feature called type inference
Summary this article explains about Generics in c#2.0.
|
No responses found. Be the first to respond and make money from revenue sharing program.
|