2. Generics Generics are similar to C++ templates. Generics has the following features: Type Safety, No boxing and unboxing when using collections for value types. No Downcasts. Generic types can be defined in one language and used from any other .NET languages (Binary code reuse). Reduced Code bloat (typed collections). Instantiated at run-time not compile time. Work for both reference and value types. Complete run-time type information. It is not possible to assign null to generic types. In this case the keyword default can be used.
3. Generics Type parameter can be applied to Classes Structures Interfaces Delegates You can also apply type parameters to methods also Type parameters can have constraints (See next slide). Some more info on Generics: T.default Null Checks Type Casts
4. Generic Class Features It is not possible to assign null to generic types. In this case the keyword default can be used. T obj = default(T); //Initializing to default. You can apply constraints on generic types. public MyGenericType<T> where T : IEnumerable { .., } Generics supports some more constraints as follow: where T : struct //This constrain says type T must be value type. where T : class // This constrain says type T must be reference type. where T : Foo //T is required to be derived from base class Foo. where T : new() // Specifies T must have default ctor. Constructor Constraint. where T : V //The type T derived from a generic type V. This constraint // is known as “Naked Type Constraint”. You can also combine multiple constraints: public class MyClass<T>where T : IFoo, new() //Here T implements IFoo and have default constructor.
5. Generics Inheritance in Generics: A generic type can implement a generic interface. Similarly generic class can be derived from a generic base class provided it must follow the below requirement. Generic type of the interface must be repeated or the type of the base class must be specified. Static Members: Static members of a generic class are only shared with one instantiation of the class. There is separate instance of the static variable exist for each type parameter. Generic Interfaces: Using generics you can define interfaces that defines methods with generic parameters. Generic Methods: Like C++, it is also possible to generic methods. Here generic type is define with method declaration. void Swap<T>(ref T x, ref T y) { …,} Swap<int>(ref i, ref j); //Invoking a generic method.
6. Generic Delegates With generic delegates the parameter of the delegate can be defined later. Simple delegate examples is given below: public delegate void Del<T>(T item); public delegateT2 Action<T1, T2>(T1 tVal); Nullable<T>: Suppose if you are instantiated as Nullable<int> x. The variable x can now be used like int and also you can assign null to it. Nullable<int> x; //or int? x; if(x.HasValue) x = 5; int y = x.Value; x += 20; x = null; EventHandler<TEventArgs>: With Windows forms, Web Applications delegates many different event handlers are defined. ArraySegment<T>: This structure is used to represent a segment of an array. The offset and count of segment is stored in this structure. Benefit of ArraySegment is you can pass it to method as a argument. Take note, ArraySegment don’t make copy of elements of array. It just refers it.