Generics

876 views

Published on

Published in: Education, Technology, Business
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
876
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
0
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Generics

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

×