Generics C#

Click here to load reader

download Generics C#

of 11

Transcript of Generics C#

  • 1.GenericsGenerics OverviewGeneric ClassGeneric interfacesGeneric StructsGeneric Methods

2. Generics Overview Generics introduce to the .NET Framework theconcept of type parameters, which make it possible todesign classes and methods that defer thespecification of one or more types until the class ormethod is declared and instantiated by client code. The .NET Framework class library contains severalnew generic collection classes in theSystem.Collections.Generic namespace. Generics allow you to define type-safe classeswithout compromising type safety, performance, orproductivity. 3. Generics Overview We use generic types to maximize Code Reuse, TypeSafety, Performance, and Code Bloat. Generics Features Default Values It is not possible to assign null to generic type You can use default keyword to initialize default (Either 0 or null) value. Constraints If Generic Class needs to invoke some methods from the generic type,you have to add constraints. Inheritance A generic type can implement a generic interface. When deriving from a generic base class, you must provide a typeargument instead of the base-classs generic type parameter. Static Members Static Members of generic class are only shared with one instantiation ofthe class. 4. Generic Class Generic classes encapsulate operations that are notspecific to a particular data type. The most common use for generic classes is withcollections like linked lists, hash tables, stacks, queues,trees, and so on. Operations such as adding and removing items from thecollection are performed in basically the same wayregardless of the type of data being stored. Syntax:public class ClassName{//Class Members} 5. Generic Class Constraints: When you define a generic class, you can apply restrictionsto the kinds of types that client code can use for typearguments when it instantiates your class. EX:class EmployeeList where T : Employee, IEmployee,System.IComparable, new(){ // ... } You can apply constraints to multiple parameters, andmultiple constraints to a single parameter. 6. Generic Interfaces Generic Interfaces It is often useful to define interfaces either for generic collection classes, or for the generic classes that represent items in the collection. We use generic interfaces to avoid boxing and unboxing operations on value types. The .NET Framework class library defines several generic interfaces for use with the collection classes in the System.Collections.Generic namespace. 7. Generic Interfaces Covariance and Contra-variance in Generics Covariant and contra-variant generic type parameters provide greater flexibility in assigning and using generic types. For example, covariant type parameters enable you to make assignments that look much like ordinary polymorphism. Covariance with Generic Interface A generic Interface is Covariant if the type is annotated with out keyword. Contra-variance with Generic Interface A generic Interface is Contra-variant if the type is annotatedwith in keyword. 8. Generic Structs Generic Structs Similar to Classes structs can be generic as well. They are similar to generic classes with theexception of inheritance features. Ex: public struct Nullable {//.} 9. Generic Methods Generic Methods A generic method is a method that is declared with type parameters, as follows: static void Swap(ref T lhs, ref T rhs) {T temp; temp = lhs; lhs = rhs; rhs = temp;} You can define method-specific generic type parameterseven if the containing class does not use generics at all 10. Generic Methods withConstraints We can have Constraints on methods also. When a method defines its own generic typeparameters, it can also define constraints for thesetypes. public void SomeMethod(T t ) where T : IComparable{...} You cannot provide method-level constraints for class-level generic type parameters. All constraints for class-level generic type parameters must be defined at theclass scope. 11. Generic Delegates A delegate defined in a class can take advantage of the generic type parameter of that class. For example: Ex: public delegate void Del(T item); public static void Notify(int i) { } Del m1 = new Del(Notify); Generic Methods Specialization Generic methods can be overloaded to define specializationsfor specific types. This is true for methods with generic parameters as well.