Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.



Published on

Published in: Technology, Business
  • Be the first to comment


  1. 1. ge⋅ner⋅ic –adjective  Also,  ge⋅ner⋅i⋅cal. 1. of, applicable to, or referring to all the members of a genus, class, group, or kind; general.
  2. 2. <ul><li>“ Generic&quot;, unrelated to the programming world, it simply means something that is not tied to any sort of brand name. </li></ul><ul><li>For example, if we purchase some generic dish soap, soap that has no brand name on it, we know that we are buying dish soap and expect it to help us clean our dishes. We can treat it as dish soap even though we don't really have any idea of its exact contents. </li></ul>
  3. 3. <ul><li>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. </li></ul><ul><li>You can write a single class that other client code can use without incurring the cost or risk of runtime casts or boxing operations </li></ul>
  4. 4. <ul><li>Generics are a new feature in version 2.0 of the common language runtime (CLR). </li></ul><ul><li>Generics use the new keyword  Of </li></ul><ul><li>By creating a generic class, you can create a collection that is type-safe at compile-time. </li></ul><ul><li>Generic classes and methods combine reusability, type safety and efficiency in a way that their non-generic counterparts cannot. </li></ul>
  5. 5. <ul><li>The most basic type of a collection is an array; </li></ul><ul><ul><li>however, arrays can be difficult to use, particularly when the number of items in the array varies and when the actual items in the array vary or move around. </li></ul></ul><ul><ul><li>To address these limitations, we have </li></ul></ul><ul><li>Collections, </li></ul><ul><ul><li>which are more or less friendly wrappers around arrays. More important, they enable us to interact with collections of types in a familiar way, just as we might interact with a collection of items in the real world. </li></ul></ul>
  6. 6. <ul><li>The problem with collections in the first version of .NET is that in order for them to be strongly typed, the developer would have to write custom collections for each and every type, overriding or implementing methods (such as Add, Remove, Contains, etc.) and indexers on each one </li></ul><ul><li>Life was also hard for the computer because in order to generalize collection types enough, it had to use the Framework base class, Object, in order to provide the needed flexibility. Value types had to put them into a special package known as a boxing in order to store them in the collection (because the collection was of the Object type, which is a reference type). </li></ul>
  7. 7. <ul><li>Any reference or value type that is added to an ArrayList is implicitly upcast to Object . </li></ul><ul><li>If the items are value types, they must be boxed when they are added to the list, and unboxed when they are retrieved. Both the casting and the boxing and unboxing operations decrease performance ; the effect of boxing and unboxing can be very significant in scenarios where you must iterate over large collections </li></ul>
  8. 8. <ul><li>The other limitation is lack of compile-time type checking; because an ArrayList casts everything to Object , there is no way at compile-time to prevent client code from doing something such as this: </li></ul>
  9. 9. <ul><li>Use generic types to maximize code reuse, type safety, and performance. </li></ul><ul><li>The most common use of generics is to create collection classes. </li></ul><ul><li>The .NET Framework class library contains several new generic collection classes in the System.Collections.Generic  namespace. These should be used whenever possible in place of classes such as ArrayList  in the  System.Collections  namespace. </li></ul>
  10. 10. <ul><li>Can create custom own generic interfaces, classes, methods, events and delegates. </li></ul><ul><li>Generic classes may be constrained to enable access to methods on particular data types. </li></ul><ul><li>Information on the types used in a generic data type may be obtained at run-time by means of reflection. </li></ul>
  11. 11. <ul><li>What a &quot;type&quot; is in .NET </li></ul><ul><ul><li>  &quot;Type&quot; is simply short hand for &quot;type of data&quot; or &quot;data type&quot;; a type tells the computer how to store information (data) and what will be done with that information. </li></ul></ul><ul><li>  What Is meant by strongly typed? </li></ul><ul><ul><li>means that the computer has enough information-a clear blueprint-on how to deal with particular data  </li></ul></ul>
  12. 12. <ul><li>What is Type Parameter </li></ul><ul><ul><li>A type parameter is the core of enabling generic functionality. </li></ul></ul><ul><ul><li>For types, it is kind of like a constructor parameter. </li></ul></ul><ul><ul><li>For methods, it's like other parameters except that it can often be inferred </li></ul></ul>
  13. 13. <ul><li>What is Generic Type? </li></ul><ul><ul><li>A definition of a class, structure, interface, procedure, or delegate for which you supply at least one data type when you declare it. </li></ul></ul><ul><li>What is Generic method?  </li></ul><ul><ul><li>A generic method is any method that defines one or more type parameters, as in the DoSomething method </li></ul></ul><ul><ul><li>It is important to note that generic methods do not have to be declared within generic types </li></ul></ul>
  14. 14. <ul><li>Type Parameter </li></ul><ul><ul><li>In a generic type definition, a placeholder for a data type you supply when you declare the type. </li></ul></ul><ul><li>Type Argument </li></ul><ul><ul><li>A specific data type that replaces a type parameter when you declare a constructed type from a generic type. </li></ul></ul>
  15. 15. <ul><li>Constraint </li></ul><ul><ul><li>A condition on a type parameter that restricts the type argument you can supply for it. </li></ul></ul><ul><ul><li>A constraint can require that the type argument must implement a particular interface, be or inherit from a particular class, have an accessible parameter less constructor, or be a reference type or a value type. You can combine these constraints, but you can specify at most one class. </li></ul></ul>
  16. 16. <ul><li>What is Constructed Type? </li></ul><ul><ul><li>This means a type that is created (constructed) as a result of the usage of a generic type. </li></ul></ul><ul><ul><li>A class, structure, interface, procedure, or delegate declared from a generic type by supplying type arguments for its type parameters. </li></ul></ul><ul><li>What are called Closed constructed types? </li></ul><ul><ul><li>At design-time we know the type that is being passed to it </li></ul></ul><ul><li>What are called open constructed types? </li></ul>
  17. 17. <ul><li>C# </li></ul><ul><ul><li>public class MySample<T> </li></ul></ul><ul><ul><li>{ </li></ul></ul><ul><ul><li>       public void DoSometing<K,V>() </li></ul></ul><ul><ul><li>       {} </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><li>VB .NET </li></ul><ul><ul><li>Public Class MySample(Of T) </li></ul></ul><ul><ul><li>       Public Sub DoSomething(Of K, V)() </li></ul></ul><ul><ul><li>       End Sub </li></ul></ul><ul><ul><li>End Class </li></ul></ul>
  18. 18. <ul><li>C# </li></ul><ul><li>MySample<string> someSample; </li></ul><ul><li>VB .NET </li></ul><ul><li>someSample As MySample(Of String) </li></ul>
  19. 19. <ul><li>C# </li></ul><ul><ul><li> public class MyExample<V> </li></ul></ul><ul><ul><li>       {private MySample<V> someSample;} </li></ul></ul><ul><li>VB .NET </li></ul><ul><ul><li>     Public Class MyExample(Of V) </li></ul></ul><ul><ul><li>       Private someSample As MySample(Of V) </li></ul></ul><ul><ul><li>     End Class </li></ul></ul>
  20. 20. <ul><li>What is Constrain in .NET Generics? </li></ul><ul><ul><li>That enable the creator of a generic to specify that only certain types may be passed in as type arguments to his generic. </li></ul></ul>
  21. 21. <ul><li>C# </li></ul><ul><ul><li>       public class MyClass<T, K> { </li></ul></ul><ul><ul><li>         public void DoSomething() { </li></ul></ul><ul><ul><li>                   T temp = new T(); </li></ul></ul><ul><ul><li>                  Debug.Assert(temp.Contains(“test”)); }} </li></ul></ul><ul><li>VB .NET </li></ul><ul><ul><ul><li>       Public Class MyClass(Of T, K) </li></ul></ul></ul><ul><ul><ul><li>             Public Sub DoSomething() </li></ul></ul></ul><ul><ul><ul><li>                   Dim temp As T = New T() </li></ul></ul></ul><ul><ul><ul><li>                  Debug.Assert(temp.Contains(“test”)) </li></ul></ul></ul><ul><ul><ul><li>             End Sub </li></ul></ul></ul><ul><ul><ul><li>       End Class </li></ul></ul></ul>
  22. 22. <ul><li>C# </li></ul><ul><ul><li>       public class MyClass<T,K> where T: IList, new() where K: Icomparable { </li></ul></ul><ul><ul><li>             public void DoSomething() { </li></ul></ul><ul><ul><li>                   T temp = new T(); </li></ul></ul><ul><ul><li>                   Debug.Assert(temp.Contains(“test”)); } }         </li></ul></ul><ul><li>VB .NET </li></ul><ul><ul><li>       Public Class MyClass(Of T As {New, IList}, K As IComparable) </li></ul></ul><ul><ul><li>             Public Sub DoSomething() </li></ul></ul><ul><ul><li>                   Dim temp As T = New T() </li></ul></ul><ul><ul><li>                   Debug.Assert(temp.Contains(“test”)) </li></ul></ul><ul><ul><li>             End Sub </li></ul></ul><ul><ul><li>       End Class </li></ul></ul>
  23. 23. <ul><li>Constraints are specified by using the  where  contextual keyword </li></ul>Constraint Description where T: struct The type argument must be a value type. Any value type except  Nullable  can be specified. See  Using Nullable Types (C# Programming Guide)  for more information. where T : class The type argument must be a reference type; this applies also to any class, interface, delegate, or array type. where T : new() The type argument must have a public parameterless constructor. When used together with other constraints, the  new()  constraint must be specified last.
  24. 24. where T : <base class name> The type argument must be or derive from the specified base class. where T : <interface name> The type argument must be or implement the specified interface. Multiple interface constraints can be specified. The constraining interface can also be generic. where T : U The type argument supplied for T must be or derive from the argument supplied for U. This is called a naked type constraint.
  25. 25. <ul><li>Which are simply methods that have type parameters. Generic methods are useful in many situations, particularly where you need the return type to be variable as in, e.g., factory methods. </li></ul><ul><li>Methods are another exciting field of use for Generics. Generic methods will allow you to pass one or more data types. </li></ul>
  26. 26. <ul><li>C# </li></ul><ul><ul><ul><li>public T DoSomething<T>(T input, string input2) { </li></ul></ul></ul><ul><ul><ul><li>  T tempVar; </li></ul></ul></ul><ul><ul><ul><li>} </li></ul></ul></ul><ul><li>VB .NET </li></ul><ul><li>Public Function DoSomething(Of T)(ByVal input As T, _ </li></ul><ul><li>ByVal input2 As String) As T </li></ul><ul><li>             Dim tempVar As T; </li></ul><ul><li>       End Function </li></ul>
  27. 27. <ul><li>System.Collections.Generic was created. It contains a lot of generic classes, structures, and interfaces like the following: </li></ul><ul><ul><li>Dictionary </li></ul></ul><ul><ul><li>List </li></ul></ul><ul><ul><li>Queue </li></ul></ul><ul><ul><li>SortedDictionary </li></ul></ul><ul><ul><li>Stack </li></ul></ul>
  28. 28. <ul><li>Generics in VB.NET are very dangerous if you do not put  Option Strict On  at the beginning of your code or as your default in the configuration. VB.NET has a tendency to autocast if strict is not on </li></ul>
  29. 29. <ul><li>Generics has the following advantages: </li></ul><ul><ul><li>The Program becomes statically typed, so errors are discovered at compile-time. </li></ul></ul><ul><ul><li>No runtime casts are required and the program runs faster. </li></ul></ul><ul><ul><li>Primitive type values (e.g int) need not be wrapped. Hence the program is faster and uses less space. </li></ul></ul>
  30. 30. <ul><li>Type Safe: </li></ul><ul><ul><li>Generic types enforce compile-time type checking. This greatly reduces, if not eliminates, unhanded run-time exceptions due to data type conflicts </li></ul></ul><ul><ul><li>Types based on  Object  accept any data type, and you must write code to check whether an input data type is acceptable. With generic types, the compiler can catch type mismatches before run time. </li></ul></ul>
  31. 31. <ul><li>Re-usability : </li></ul><ul><ul><li>A class written with Generics is reusable, no need duplicating classes for different data types </li></ul></ul><ul><li>Performance: </li></ul><ul><ul><li>Since the data type is determined at compile-time there is no type casting at run-time </li></ul></ul><ul><ul><li>Generic types do not have to  box  and  unbox  data, because each one is specialized for one data type. </li></ul></ul>
  32. 32. <ul><li>Code Consolidation.   </li></ul><ul><ul><li>The code in a generic type has to be defined only once. </li></ul></ul><ul><ul><li>A set of type-specific versions of a type must replicate the same code in each version, with the only difference being the specific data type for that version. </li></ul></ul><ul><ul><li>With generic types, the type-specific versions are all generated from the original generic type. </li></ul></ul>
  33. 33. <ul><li>Code Reuse.   </li></ul><ul><ul><li>Code that does not depend on a particular data type can be reused with various data types if it is generic. You can often reuse it even with a data type that you did not originally predict. </li></ul></ul><ul><li>Generic Algorithms. </li></ul><ul><ul><li>Abstract algorithms that are type-independent are good candidates for generic types. For example, a generic procedure that sorts items using the  IComparable  interface can be used with any data type that implements  IComparable . </li></ul></ul>
  34. 34. <ul><li>IDE Support.   </li></ul><ul><ul><li>When you use a constructed type declared from a generic type, the integrated development environment (IDE) can give you more support while you are developing your code. </li></ul></ul><ul><ul><li>For example, IntelliSense can show you the type-specific options for an argument to a constructor or method. </li></ul></ul>
  35. 35. <ul><li>Since generics without constraints are perceived by the compiler to be System.Object types, you cannot access any type members not available to System.Object nor can you use arithmetic operators or nested classes. </li></ul><ul><li>Cannot access static members or nested classes without explicitly casting. </li></ul><ul><li>Cannot use arithmetic operations unless the base class specified in the constraints list supports them. </li></ul><ul><li>Cannot use generics themselves as type arguments, but, you can use constructed types as type arguments. </li></ul>
  36. 36. <ul><li>Creating Generic Type </li></ul><ul><ul><li>public class Col<T> { T t; public T Val{get{return t;}set{t=value;}} </li></ul></ul><ul><ul><ul><li>There are a few things to notice. </li></ul></ul></ul><ul><ul><li>The class name &quot;Col<T>&quot; is our first indication that this Type is generic. </li></ul></ul><ul><ul><li>This Type placeholder &quot;T&quot; is used to show that if we need to refer to the actual Type that is going to be used when we write this class, we will represent it as &quot;T&quot;. </li></ul></ul><ul><ul><li>Notice on the next line the variable declaration &quot;T t;&quot; creates a member variable with the type of T, or the generic Type which we will specify later during construction of the class (it will actually get inserted by the Common Language Runtime (CLR) automatically for us). </li></ul></ul><ul><ul><li>The final item in the class is the public property. Again, notice that we are using the Type placeholder &quot;T&quot; to represent that generic type for the type of that property. Also notice that we can freely use the private variable &quot;t&quot; within the class. </li></ul></ul>
  37. 37. <ul><li>Use of the Previous type in a Class </li></ul><ul><li>public class ColMain </li></ul><ul><li>{ </li></ul><ul><li>public static void Main() </li></ul><ul><ul><li>{ //create a string version of our generic class </li></ul></ul><ul><ul><li>Col<string> mystring = new Col<string>(); </li></ul></ul><ul><ul><li>mystring.Val = &quot;hello&quot;; //output that value System.Console.WriteLine(mystring.Val); System.Console.WriteLine(mystring.Val.GetType()); //create another instance of our generic class, using a different type </li></ul></ul><ul><ul><li>Col<int> myint = new Col<int>(); </li></ul></ul><ul><ul><li>myint.Val = 5; </li></ul></ul><ul><ul><li>System.Console.WriteLine(myint.Val); </li></ul></ul><ul><ul><li>System.Console.WriteLine(myint.Val.GetType()); </li></ul></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  38. 38. <ul><li>For this example we want to be able to hold a collection of a Type that we create, which we used to represent a User in our system. Here is the definition for that class: </li></ul><ul><ul><li>namespace Rob </li></ul></ul><ul><ul><li>{ </li></ul></ul><ul><ul><ul><li>public class User </li></ul></ul></ul><ul><ul><ul><li>{ </li></ul></ul></ul><ul><ul><ul><ul><ul><li>protected string name; </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>protected int age; </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>public string Name{get{return name;}set{name=value;}} </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>public int Age{get{return age;}set{age=value;}} </li></ul></ul></ul></ul></ul><ul><ul><ul><li>} </li></ul></ul></ul><ul><ul><li>} </li></ul></ul>
  39. 39. <ul><li>Now that we have our User defined, let's create an instance of the .NET Framework Generic version of a simple List: </li></ul><ul><li>System.Collections.Generic.List<Rob.User> users = new System.Collections.Generic.List<Rob.User>(); </li></ul><ul><ul><li>For ( int x=0;x<5;x++) </li></ul></ul><ul><ul><ul><li>{ </li></ul></ul></ul><ul><ul><ul><ul><li>Rob.User user = new Rob.User(); </li></ul></ul></ul></ul><ul><ul><ul><ul><li>user.Name=&quot;Rob&quot; + x; </li></ul></ul></ul></ul><ul><ul><ul><ul><li>user.Age=x; </li></ul></ul></ul></ul><ul><ul><ul><ul><li>users.Add(user); </li></ul></ul></ul></ul><ul><ul><ul><li>} </li></ul></ul></ul>
  40. 40. <ul><li>Public class Dictionary<K, V> where K : IComparable {} </li></ul><ul><li>Notice the &quot;where&quot; clause on this class definition. It is forcing the K Type to be of Type IComparable. </li></ul><ul><li>If K does NOT implement IComparable, you will get a Compiler error. </li></ul>
  41. 41. <ul><li>Another type of constraint is a constructor constraint: </li></ul><ul><li>public class Something<V> where V: new() {} </li></ul>
  42. 42. <ul><li> </li></ul><ul><li> </li></ul><ul><li> </li></ul><ul><li> </li></ul><ul><li> </li></ul>