Your SlideShare is downloading. ×
0
 
C# Generics
C# Generics <ul><ul><li>Pre-requisites </li></ul></ul><ul><ul><li>C# </li></ul></ul><ul><ul><li>OO  </li></ul></ul><ul><ul...
Overview <ul><ul><li>“ Generic classes and methods  </li></ul></ul><ul><ul><li>defer the specification of one or more  </l...
Life without Generics public   class   ExampleStack { private   object [] contents =  new   object [99]; private   int  pt...
Life without Generics public   class   ExampleStack { private   object [] contents =  new   object [99]; private   int  pt...
Life with Generics public   class   ExampleStack <T> { private  T[] contents =  new  T[99]; private   int  ptr; public   v...
Life with Generics public   class   ExampleStack <T> { private  T[] contents =  new  T[99]; private   int  ptr; public   v...
The 'Where' Clause  public   class   ExampleStack <T> where  T: IComparable { private  T[] contents =  new  T[99]; private...
Multiple Generics  public   class   ExampleStack <Tinterface, Tclass>  where  Tclass : Tinterface  where  Tinterface : ICl...
Generics are not just for classes!  public   interface   Interface <T>  { bool  DoWork(T workItem); } public   interface  ...
Generics are not just for classes!  public   class   Example  { public  T GenericMethod<T>(T obj1 , T obj2)  where  T: ICo...
Native Generics  <ul><li>Namespace System.Collections.Generic </li></ul><ul><ul><li>List <T> :  </li></ul></ul><ul><ul><li...
Summary <ul><ul><li>“ Generic classes and methods  </li></ul></ul><ul><ul><li>defer the specification of one or more  </li...
Upcoming SlideShare
Loading in...5
×

Generics

547

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
547
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Transcript of "Generics"

  1. 2. C# Generics
  2. 3. C# Generics <ul><ul><li>Pre-requisites </li></ul></ul><ul><ul><li>C# </li></ul></ul><ul><ul><li>OO </li></ul></ul><ul><ul><li>Interfaces </li></ul></ul><ul><ul><li>Stack data structure </li></ul></ul>
  3. 4. Overview <ul><ul><li>“ Generic classes and methods </li></ul></ul><ul><ul><li>defer the specification of one or more </li></ul></ul><ul><ul><li>types until declaration by client code ” </li></ul></ul>
  4. 5. Life without Generics public class ExampleStack { private object [] contents = new object [99]; private int ptr ; public void Push( object o) { contents[++ptr] = o; } public object Pop() { return contents[ptr--]; } } What can go wrong ? ExampleStack stack2 = new ExampleStack (); stack2.Push( &quot;A&quot; ); stack2.Push( &quot;B&quot; ); stack2.Push( &quot;C&quot; ); Console .WriteLine(stack2.Pop()); Console .WriteLine(stack2.Pop()); Console .WriteLine(stack2.Pop());
  5. 6. Life without Generics public class ExampleStack { private object [] contents = new object [99]; private int ptr ; public void Push( object o) { contents[++ptr] = o; } public object Pop() { return contents[ptr--]; } } What can go wrong ? ExampleStack stack2 = new ExampleStack (); stack2.Push( &quot;A&quot; ); stack2.Push( null ); stack2.Push( 12.7 ); Console .WriteLine(stack2.Pop()); Console .WriteLine(stack2.Pop()); Console .WriteLine(stack2.Pop());
  6. 7. Life with Generics public class ExampleStack <T> { private T[] contents = new T[99]; private int ptr; public void Push(T o) { contents[++ptr] = o; } public T Pop() { return contents[ptr--]; } } What can go wrong ? ExampleStack < int > stack1 = new ExampleStack < int >(); stack1.Push(32); stack1.Push(99); stack1.Push(101); // Wont compile // stack1.Push(&quot;string &quot;); // stack1.Push('a'); Console .WriteLine(stack1.Pop()); Console .WriteLine(stack1.Pop()); Console .WriteLine(stack1.Pop());
  7. 8. Life with Generics public class ExampleStack <T> { private T[] contents = new T[99]; private int ptr; public void Push(T o) { contents[++ptr] = o; } public T Pop() { return contents[ptr--]; } } <ul><li>What can go wrong ? ExampleStack < int > stack1 = new ExampleStack < int >(); </li></ul><ul><li>stack1.Push(32); stack1.Push(99); stack1.Push(101); // Wont compile // stack1.Push(&quot;string &quot;); // stack1.Push('a'); Console .WriteLine(stack1.Pop()); Console .WriteLine(stack1.Pop()); Console .WriteLine(stack1.Pop()); </li></ul><ul><ul><li>This is type safe. </li></ul></ul><ul><ul><li>Compile time errors not run time errors </li></ul></ul><ul><ul><li>Faster Execution! </li></ul></ul>
  8. 9. The 'Where' Clause public class ExampleStack <T> where T: IComparable { private T[] contents = new T[99]; private int ptr; public void Push(T o) { contents[++ptr] = o; } public T Pop() { return contents[ptr--]; } } Similar to SQL 'where' Enforces compile time checks on the generic May be Interfaces May enforce constructors E.G. new().new(int)
  9. 10. Multiple Generics public class ExampleStack <Tinterface, Tclass> where Tclass : Tinterface where Tinterface : ICloneable { private Tclass[] contents = new Tclass[99]; private int ptr; public void Push(Tclass o) { contents[++ptr] = o; } public Tinterface Pop() { return contents[ptr--]; } } Any number of Generic classes can be added ExampleStack < ICloneable , string > stack1 = new ExampleStack < ICloneable , string >(); stack1.Push( &quot;Some String&quot; ); ICloneable clone = stack1.Pop();
  10. 11. Generics are not just for classes! public interface Interface <T> { bool DoWork(T workItem); } public interface InterfaceString : Interface < string > { // now has method // bool DoWork(string workItem); } public class Implemetation : InterfaceString { public bool DoWork( string workItem) { // Do work return false ; } } Generics are not just for classes Interfaces Methods NOT Properties and Fields directly
  11. 12. Generics are not just for classes! public class Example { public T GenericMethod<T>(T obj1 , T obj2) where T: IComparable { if (obj1.CompareTo(obj2) > 0) return obj1; else return obj2; } } Generics are not just for classes Interfaces Methods NOT Properties and Fields directly
  12. 13. Native Generics <ul><li>Namespace System.Collections.Generic </li></ul><ul><ul><li>List <T> : </li></ul></ul><ul><ul><li>IList <T>, </li></ul></ul><ul><ul><li>ICollection <T>, </li></ul></ul><ul><ul><li>IEnumerable <T> </li></ul></ul>Used throughout C# to enforce type safety
  13. 14. Summary <ul><ul><li>“ Generic classes and methods </li></ul></ul><ul><ul><li>defer the specification of one or more </li></ul></ul><ul><ul><li>types until declaration by client code ” </li></ul></ul>
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×