Thursday, August 9, 2007

Generics

GENERICS
• New feature in .net 2.0 and CLR
• Found in Namespace: System.Collections.Generic
• Generics are also called parameterized types or parameteric polymorphism
• Generics are a type of data structure that contain code that remains the same; however the data type of the parameters can change with each use
• Generic helps to defer the binding of a generic type to a data type until its point of usage arrives
• Generics introduce to the .net framework the concept of type parameters, which make it possible to design classes and methods that defer the specification of one or more types until the class or method is declared and instantiated by client code
• Generics in C# support defining algorithms and deferring the data type until the point of use
• In a very general sense, a generic class is just a class that accepts parameters
• Generics allow strongly typed collections, hence letting the compiler do the type checking and hence preventing run time errors caused by type mismatch/casting
• Syntax: Without Generics: class List {} With Generics: class List
• Here T is the Type parameter, T gets defined when object if List is created, hence it can be List or List
• Hence, here T is instantiating at runtime

No comments: