About Generics
Generics are part of the .NET framework's type system that allows us to define a type while leaving some details unspecified. Instead of specifying the types of parameters or member classes we can allow code that uses our type to specify it. This allows consumer code to tailor the type to its own specific needs. The .NET framework version 2.0 includes several generic classes in the System.Collections. Generic namespace, including dictionary, queue, sorted dictionary, and sorted list. These classes work similarly to their non-generic counter parts in system.colluccection, but they offer improved performance and type safety. Developers used the object classes for parameters and members and would cast other classes to and from the object class. Generics offer two significant advantages over using the object class :
- Reduced Run-time errors: The compiler cannot detect type errors when we cast to and from the object class. Run-time errors will throw an exception. Using generics allow compilers to catch the type of bug before the program runs.
- Improved Performance: Casting requires boxing and un-boxing that steals processor time and slows performance. Using generics doesn't require casting or boxing which improve run- time performance.
No comments:
Post a Comment