Generics allow you to define type-safe classes without compromising type safety, performance, or productivity.
Generics Benefits
1. Generics in .NET let you reuse code and the effort you put into implementing it. The types and internal data can change without causing code bloat, regardless of whether you are using value or reference types.
2. Reduced Run time errors. In an object-based solutions, compiler cannot detect any errors during compile time in the case of wrong cast operation, as every type in .Net is derived from object class. But this will cause run-time error. By using the Generics, this situation can be avoided as compiler checks for type-safety.
3. When using value types, the casting between Object and appropriate value type causes unnecessary boxing and un-boxing operations which incur significant performance penalty. Even when using reference types, the casting between object to the actual type involves some casting cost. These costs can be avoided using generics.
No comments:
Post a Comment