Everything You Never Wanted to Know About .NET Generics (But Were Afraid Someone Would Ask) Copyright © 2007 by Martin L. ...
<ul><li>Software Developer since 1985. </li></ul><ul><ul><li>Windows developer since 1989. </li></ul></ul><ul><ul><li>UML ...
Agenda <ul><li>Lots of Boxes, or Just Box? </li></ul><ul><li>Three “Solutions” </li></ul><ul><li>Generics </li></ul><ul><l...
Lots of Boxes…? <ul><li>Different kinds of boxes…? </li></ul>Copyright © 2007 by Martin L. Shoemaker
… or Just Box? <ul><li>A place to put stuff. </li></ul><ul><li>A lid that opens and closes. </li></ul><ul><li>A latch that...
Lots of Boxes, or Just Box? <ul><li>But what if you mix up the boxes? </li></ul>Copyright © 2007 by Martin L. Shoemaker
Four “Solutions” <ul><li>Inheritance </li></ul><ul><li>Typeless </li></ul><ul><li>Interfaces </li></ul><ul><li>Wrappers </...
Four “Solutions”: Inheritance <ul><li>Inherit contents from a base class: </li></ul><ul><li>class Contents; </li></ul><ul>...
Four “Solutions”: Inheritance <ul><li>Problems: </li></ul><ul><ul><li>Horribly abuses inheritance. </li></ul></ul><ul><ul>...
Four “Solutions”: Typeless <ul><li>Inherit contents from  the  base class, object: </li></ul><ul><li>class Food {} </li></...
Four “Solutions”: Typeless Copyright © 2007 by Martin L. Shoemaker
Four “Solutions”: Interfaces <ul><li>Content classes implement an interface: </li></ul><ul><li>interface IContents; </li><...
Four “Solutions”: Interfaces Copyright © 2007 by Martin L. Shoemaker
Four “Solutions”: Wrappers <ul><li>Wrap the box class: </li></ul><ul><li>class Box </li></ul><ul><li>{ public object stuff...
Four “Solutions”: Wrappers Copyright © 2007 by Martin L. Shoemaker
Generics <ul><li>Classes, methods, interfaces, delegates, and events designed to work with one or more “placeholder” types...
Generics <ul><li>Don’t abuse inheritance  or  interfaces. </li></ul><ul><li>Don’t require the designers of Food and Tool t...
Generics <ul><li>Examples: </li></ul><ul><li>Calendar(Of T) </li></ul><ul><li>Lunch Menu </li></ul><ul><li>Work Schedule <...
Generics vs. C++ Templates <ul><li>Generics are inspired by C++ templates, but…  </li></ul><ul><li>Generics do not support...
Generics vs. C++ Templates <ul><li>Generics are inspired by C++ templates, but (continued)…  </li></ul><ul><li>A generic t...
.NET Generics vs. Java Generics <ul><li>Did you see the word “Java”  anywhere  on my bio slide? </li></ul><ul><li>I have a...
Generic Delegates and Events <ul><li>Generics aren’t just for classes and interfaces. </li></ul><ul><li>Generic delegates ...
Constraining Generic Parameters <ul><li>When you declare a generic, you can constrain the parameters to certain types. </l...
Constraining Generic Parameters <ul><li>Examples… </li></ul>Copyright © 2007 by Martin L. Shoemaker
The  Boring  Generics Everybody Talks About… <ul><li>… i.e., collections. </li></ul><ul><li>The basic collection interface...
The  Boring  Generics: The Basic Collection Interfaces <ul><li>Not very useful by themselves, but the useful collections b...
The  Boring  Generics: The List Collections <ul><li>IList. Basic list behavior: ICollection<T>; IEnumerable<T>; Item; Inde...
The  Boring  Generics: The List Collections <ul><li>SynchronizedCollection. Thread-safe implementation of IList. </li></ul...
The  Boring  Generics: The List Collections <ul><li>Example: </li></ul><ul><li>Calendar(Of T) </li></ul>Copyright © 2007 b...
The  Boring  Generics: The Keyed Collections <ul><li>IDictionary. Defines key/value lookup behavior: Item; Keys; Values; A...
The  Boring  Generics: The Keyed Collections <ul><li>SortedList. Small memory binary-tree implementation of IDictionary an...
The  Boring  Generics: The Keyed Collections <ul><li>Example: </li></ul><ul><li>Calendar(Of T) </li></ul>Copyright © 2007 ...
The  Cool  Generics Nobody Ever Talks About <ul><li>Comparison Interfaces, Classes, and Delegates </li></ul><ul><li>Other ...
The  Cool  Generics: Comparison Interfaces, Classes, and Delegates <ul><li>IComparable(Of T). Indicates a type can be comp...
The  Cool  Generics: Comparison Interfaces, Classes, and Delegates <ul><li>IEquatable(Of T): Indicates a type can be teste...
The  Cool  Generics: Comparison Interfaces, Classes, and Delegates <ul><li>Examples… </li></ul>Copyright © 2007 by Martin ...
The  Cool  Generics: Other Delegates <ul><li>Action(Of T). </li></ul><ul><ul><li>Delegate for a method that operates on an...
The  Cool  Generics: Other Delegates <ul><li>Predicate(Of T). </li></ul><ul><ul><li>Delegate for a method that determines ...
The  Cool  Generics: Other Delegates <ul><li>Predicate(Of T). </li></ul><ul><ul><li>List(Of T) and Array: </li></ul></ul><...
The  Cool  Generics: Other Delegates <ul><li>Converter(Of TInput, TOutput). </li></ul><ul><ul><li>Converts objects of one ...
The  Cool  Generics: Miscellaneous <ul><li>Nullable </li></ul><ul><li>ArraySegment </li></ul>Copyright © 2007 by Martin L....
Generics and Serialization <ul><li>The bad news: XML serialization doesn’t support generics. </li></ul><ul><li>The good ne...
Conclusion <ul><li>Generics define classes, methods, interfaces, delegates, and events independent of the types they opera...
Questions... <ul><li>[email_address] </li></ul>Copyright © 2007 by Martin L. Shoemaker
Everything You Never Wanted to Know About .NET Generics (But Were Afraid Someone Would Ask) Copyright © 2007 by Martin L. ...
Upcoming SlideShare
Loading in …5
×

Everything You Never Wanted To Know About Net Generics

897 views

Published on

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

  • Be the first to like this

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

No notes for slide

Everything You Never Wanted To Know About Net Generics

  1. 1. Everything You Never Wanted to Know About .NET Generics (But Were Afraid Someone Would Ask) Copyright © 2007 by Martin L. Shoemaker
  2. 2. <ul><li>Software Developer since 1985. </li></ul><ul><ul><li>Windows developer since 1989. </li></ul></ul><ul><ul><li>UML designer and trainer since 1997. </li></ul></ul><ul><li>Founder of Inkon Software ( http://www.InkonSoftware.com ): </li></ul><ul><ul><li>Tablet PC and .NET software consulting. </li></ul></ul><ul><ul><li>Inkon Logbook: An extensible platform for design, data gathering, and note-taking applications using pen and voice. </li></ul></ul><ul><ul><li>Tablet UML, a UML tool for the Tablet PC ( http://www.TabletUML.com ). </li></ul></ul><ul><li>Trainer & Creator of UML BootCamp, Tablet PC BootCamp, and other courses for the Richard Hale Shaw Group ( http://www.RichardHaleShawGroup.com ). </li></ul><ul><li>Author of UML Applied: A .NET Perspective from Apress and Requirements Patterns and Antipatterns , coming soon from Addison-Wesley. </li></ul><ul><li>Contact: [email_address] </li></ul>BIO: Martin L. Shoemaker Copyright © 2007 by Martin L. Shoemaker
  3. 3. Agenda <ul><li>Lots of Boxes, or Just Box? </li></ul><ul><li>Three “Solutions” </li></ul><ul><li>Generics </li></ul><ul><li>Generics vs. C++ Templates </li></ul><ul><li>.NET Generics vs. Java Generics </li></ul><ul><li>Generic Delegates and Events </li></ul><ul><li>Constraining Generic Parameters </li></ul><ul><li>The Boring Generics Everybody Talks About </li></ul><ul><li>The Cool Generics Nobody Ever Talks About </li></ul><ul><li>Generics and Serialization </li></ul>Copyright © 2007 by Martin L. Shoemaker
  4. 4. Lots of Boxes…? <ul><li>Different kinds of boxes…? </li></ul>Copyright © 2007 by Martin L. Shoemaker
  5. 5. … or Just Box? <ul><li>A place to put stuff. </li></ul><ul><li>A lid that opens and closes. </li></ul><ul><li>A latch that latches or unlatches. </li></ul><ul><li>Who cares what’s in it? It’s a box! </li></ul>Copyright © 2007 by Martin L. Shoemaker
  6. 6. Lots of Boxes, or Just Box? <ul><li>But what if you mix up the boxes? </li></ul>Copyright © 2007 by Martin L. Shoemaker
  7. 7. Four “Solutions” <ul><li>Inheritance </li></ul><ul><li>Typeless </li></ul><ul><li>Interfaces </li></ul><ul><li>Wrappers </li></ul>Copyright © 2007 by Martin L. Shoemaker
  8. 8. Four “Solutions”: Inheritance <ul><li>Inherit contents from a base class: </li></ul><ul><li>class Contents; </li></ul><ul><li>class Food : Contents {} </li></ul><ul><li>class Tool : Contents {} </li></ul><ul><li>etc… </li></ul><ul><li>Contain the base class: </li></ul><ul><li>class Box </li></ul><ul><li>{ public Contents stuff; } </li></ul>Copyright © 2007 by Martin L. Shoemaker
  9. 9. Four “Solutions”: Inheritance <ul><li>Problems: </li></ul><ul><ul><li>Horribly abuses inheritance. </li></ul></ul><ul><ul><li>Requires the designers of Food and Tool to know that the classes will be put in Boxes. </li></ul></ul><ul><ul><li>In a single-inheritance environment (like .NET), ties the designer’s hands. </li></ul></ul><ul><ul><li>Doesn’t solve the Ice Cream in the Tool Box problem. </li></ul></ul>Copyright © 2007 by Martin L. Shoemaker
  10. 10. Four “Solutions”: Typeless <ul><li>Inherit contents from the base class, object: </li></ul><ul><li>class Food {} </li></ul><ul><li>class Tool {} </li></ul><ul><li>etc… </li></ul><ul><li>Contain the base class: </li></ul><ul><li>class Box </li></ul><ul><li>{ public object stuff; } </li></ul>Copyright © 2007 by Martin L. Shoemaker
  11. 11. Four “Solutions”: Typeless Copyright © 2007 by Martin L. Shoemaker
  12. 12. Four “Solutions”: Interfaces <ul><li>Content classes implement an interface: </li></ul><ul><li>interface IContents; </li></ul><ul><li>class Food : IContents {} </li></ul><ul><li>class Tool : IContents {} </li></ul><ul><li>etc… </li></ul><ul><li>Contain the interface: </li></ul><ul><li>class Box </li></ul><ul><li>{ public IContents stuff; } </li></ul>Copyright © 2007 by Martin L. Shoemaker
  13. 13. Four “Solutions”: Interfaces Copyright © 2007 by Martin L. Shoemaker
  14. 14. Four “Solutions”: Wrappers <ul><li>Wrap the box class: </li></ul><ul><li>class Box </li></ul><ul><li>{ public object stuff; } </li></ul><ul><li>class ToolBox : Box </li></ul><ul><li>{ </li></ul><ul><li>public void SetTool(Tool tool) </li></ul><ul><li>{ stuff = tool; } </li></ul><ul><li>public Tool GetTool() </li></ul><ul><li>{ return stuff as Tool; } </li></ul><ul><li>} </li></ul>Copyright © 2007 by Martin L. Shoemaker
  15. 15. Four “Solutions”: Wrappers Copyright © 2007 by Martin L. Shoemaker
  16. 16. Generics <ul><li>Classes, methods, interfaces, delegates, and events designed to work with one or more “placeholder” types. </li></ul><ul><li>Placeholders are filled in when you use the generic. </li></ul><ul><li>class Box<T> { public T stuff; } </li></ul><ul><li>… </li></ul><ul><li>Box<Tool> toolbox = new Box<Tool>(); </li></ul><ul><li>toolbox.Stuff = new Hammer(); //OK. </li></ul><ul><li>toolbox.Stuff = new IceCreamSandwich(); //Compiler error. </li></ul>Copyright © 2007 by Martin L. Shoemaker
  17. 17. Generics <ul><li>Don’t abuse inheritance or interfaces. </li></ul><ul><li>Don’t require the designers of Food and Tool to know that the classes will be put in Boxes. </li></ul><ul><li>Don’t tie the designer’s hands. </li></ul><ul><li>Solves the Ice Cream in the Tool Box problem: the compiler type-checks. </li></ul><ul><li>Not a kludge! It’s a language/environment construct. </li></ul><ul><li>Easy! It’s a language/environment construct. </li></ul>Copyright © 2007 by Martin L. Shoemaker
  18. 18. Generics <ul><li>Examples: </li></ul><ul><li>Calendar(Of T) </li></ul><ul><li>Lunch Menu </li></ul><ul><li>Work Schedule </li></ul>Copyright © 2007 by Martin L. Shoemaker
  19. 19. Generics vs. C++ Templates <ul><li>Generics are inspired by C++ templates, but… </li></ul><ul><li>Generics do not support non-type parameters (i.e., integers). </li></ul><ul><li>Generics do not support specialization (i.e., custom implementations templates for specific types). </li></ul><ul><li>Generics cannot use a parameter type as a base class. </li></ul><ul><li>Generics cannot have default types. </li></ul>Copyright © 2007 by Martin L. Shoemaker
  20. 20. Generics vs. C++ Templates <ul><li>Generics are inspired by C++ templates, but (continued)… </li></ul><ul><li>A generic type parameter cannot itself be a generic. </li></ul><ul><li>C++ allows code that might be invalid for some types, and then checks on use. .NET generics require all code to be valid for all valid types. (See constraints, below.) </li></ul>Copyright © 2007 by Martin L. Shoemaker
  21. 21. .NET Generics vs. Java Generics <ul><li>Did you see the word “Java” anywhere on my bio slide? </li></ul><ul><li>I have absolutely no knowledge of Java generics. </li></ul><ul><li>Java folks have told me there’s a lot of similarity. </li></ul><ul><li>It is an important question. </li></ul><ul><ul><li>Feedback from the floor? </li></ul></ul>Copyright © 2007 by Martin L. Shoemaker
  22. 22. Generic Delegates and Events <ul><li>Generics aren’t just for classes and interfaces. </li></ul><ul><li>Generic delegates can be filled by any methods which match the signatures. </li></ul><ul><li>Events can be defined for generic delegates. </li></ul><ul><li>Example: </li></ul><ul><ul><li>CalendarChange(Of T) </li></ul></ul>Copyright © 2007 by Martin L. Shoemaker
  23. 23. Constraining Generic Parameters <ul><li>When you declare a generic, you can constrain the parameters to certain types. </li></ul><ul><li>The generic code can then call methods, properties, etc., implied by the constraints. </li></ul><ul><li>Constraints: </li></ul><ul><ul><li>as Structure. Type must be value type. </li></ul></ul><ul><ul><li>as Class. Type must be reference type. </li></ul></ul><ul><ul><li>as New. Type must have a default constructor. </li></ul></ul><ul><ul><li>as Base. Type must inherit from Base. (May be another type parameter.) </li></ul></ul><ul><ul><li>as IBase. Type must implement IBase. </li></ul></ul>Copyright © 2007 by Martin L. Shoemaker
  24. 24. Constraining Generic Parameters <ul><li>Examples… </li></ul>Copyright © 2007 by Martin L. Shoemaker
  25. 25. The Boring Generics Everybody Talks About… <ul><li>… i.e., collections. </li></ul><ul><li>The basic collection interfaces. </li></ul><ul><li>The list collections. </li></ul><ul><li>The keyed collections. </li></ul>Copyright © 2007 by Martin L. Shoemaker
  26. 26. The Boring Generics: The Basic Collection Interfaces <ul><li>Not very useful by themselves, but the useful collections build on them. </li></ul><ul><li>ICollection<T>. Basic collection behavior: Count, IsReadOnly, Add, Clear, Contains, CopyTo, Remove. </li></ul><ul><li>IEnumerable<T>. Means a collection may be “walked” via GetEnumerator. </li></ul><ul><li>IEnumerator<T>. Walks a collection: Current; MoveNext; Reset . </li></ul>Copyright © 2007 by Martin L. Shoemaker
  27. 27. The Boring Generics: The List Collections <ul><li>IList. Basic list behavior: ICollection<T>; IEnumerable<T>; Item; IndexOf ; Insert; RemoveAt. </li></ul><ul><li>List. Stock implementation of IList. </li></ul><ul><li>LinkedList. Doubly linked list. </li></ul><ul><li>Queue. A FIFO list. </li></ul><ul><li>Stack. A LIFO list. </li></ul>Copyright © 2007 by Martin L. Shoemaker
  28. 28. The Boring Generics: The List Collections <ul><li>SynchronizedCollection. Thread-safe implementation of IList. </li></ul><ul><li>SynchronizedReadOnlyCollection . Thread-safe implementation of Ilist, but throws an exception at any attempt to change the list. </li></ul><ul><ul><li>Um, hello? What about compile-time checking? </li></ul></ul>Copyright © 2007 by Martin L. Shoemaker
  29. 29. The Boring Generics: The List Collections <ul><li>Example: </li></ul><ul><li>Calendar(Of T) </li></ul>Copyright © 2007 by Martin L. Shoemaker
  30. 30. The Boring Generics: The Keyed Collections <ul><li>IDictionary. Defines key/value lookup behavior: Item; Keys; Values; Add; ContainsKey; Remove; TryGetValue . </li></ul><ul><li>Dictionary. Stock implementation of IDictionary. </li></ul><ul><li>KeyedByTypeCollection. Implementation of Idictionary, with types as keys. </li></ul>Copyright © 2007 by Martin L. Shoemaker
  31. 31. The Boring Generics: The Keyed Collections <ul><li>SortedList. Small memory binary-tree implementation of IDictionary and IList. </li></ul><ul><li>SortedDictionary. Fast insertion binary-tree implementation of IDictionary and IList. </li></ul><ul><li>SynchronizedKeyedCollection. Thread-safe implementation of IDictionary. </li></ul>Copyright © 2007 by Martin L. Shoemaker
  32. 32. The Boring Generics: The Keyed Collections <ul><li>Example: </li></ul><ul><li>Calendar(Of T) </li></ul>Copyright © 2007 by Martin L. Shoemaker
  33. 33. The Cool Generics Nobody Ever Talks About <ul><li>Comparison Interfaces, Classes, and Delegates </li></ul><ul><li>Other Delegates </li></ul><ul><li>Miscellaneous </li></ul>Copyright © 2007 by Martin L. Shoemaker
  34. 34. The Cool Generics: Comparison Interfaces, Classes, and Delegates <ul><li>IComparable(Of T). Indicates a type can be compared to a particular type (usually itself) using CompareTo. </li></ul><ul><li>Comparison(Of T). Delegate for comparing two instances of a particular type using Compare. </li></ul><ul><li>IComparer(Of T). Indicates a type that can compare two instances of a particular type using Compare. </li></ul><ul><li>Comparer(Of T). A stock implementation of IComparer. </li></ul>Copyright © 2007 by Martin L. Shoemaker
  35. 35. The Cool Generics: Comparison Interfaces, Classes, and Delegates <ul><li>IEquatable(Of T): Indicates a type can be tested for equality with a particular type (usually itself) using Equals. </li></ul><ul><li>IEqualityComparer(Of T): Indicates a type that can test two instances of a particular type for equality using Equals. </li></ul><ul><li>EqualityComparer(Of T). A stock implementation of IEqualityComparer. </li></ul>Copyright © 2007 by Martin L. Shoemaker
  36. 36. The Cool Generics: Comparison Interfaces, Classes, and Delegates <ul><li>Examples… </li></ul>Copyright © 2007 by Martin L. Shoemaker
  37. 37. The Cool Generics: Other Delegates <ul><li>Action(Of T). </li></ul><ul><ul><li>Delegate for a method that operates on an object of type T. </li></ul></ul><ul><ul><li>List(Of T).ForEach and Array.ForEach take an Action(Of T) delegate and automates walking the list. </li></ul></ul><ul><ul><li>Example… </li></ul></ul>Copyright © 2007 by Martin L. Shoemaker
  38. 38. The Cool Generics: Other Delegates <ul><li>Predicate(Of T). </li></ul><ul><ul><li>Delegate for a method that determines whether an object of type T meets some criterion. </li></ul></ul><ul><ul><li>List(Of T) and Array: </li></ul></ul><ul><ul><ul><li>Exists. Does any element meet the criterion? </li></ul></ul></ul><ul><ul><ul><li>Find. Find an element that meets the criterion. </li></ul></ul></ul><ul><ul><ul><li>FindAll. Get a list of all elements that meet the criterion. </li></ul></ul></ul><ul><ul><ul><li>FindIndex. Find the index of an element that meets the criterion. </li></ul></ul></ul>Copyright © 2007 by Martin L. Shoemaker
  39. 39. The Cool Generics: Other Delegates <ul><li>Predicate(Of T). </li></ul><ul><ul><li>List(Of T) and Array: </li></ul></ul><ul><ul><ul><li>FindLast. Find the last element that meets the criterion. </li></ul></ul></ul><ul><ul><ul><li>FindLastIndex. Find the index of the last element that meets the criterion. </li></ul></ul></ul><ul><ul><ul><li>RemoveAll. Removes all elements that meet the criterion. </li></ul></ul></ul><ul><ul><ul><li>TrueForAll. True if all elements meet the criterion. </li></ul></ul></ul><ul><ul><li>Examples… </li></ul></ul>Copyright © 2007 by Martin L. Shoemaker
  40. 40. The Cool Generics: Other Delegates <ul><li>Converter(Of TInput, TOutput). </li></ul><ul><ul><li>Converts objects of one type to another. </li></ul></ul><ul><ul><li>Used by List(Of T).ConvertAll and Array.ConvertAll. </li></ul></ul><ul><ul><li>Example… </li></ul></ul><ul><li>EventHandler(Of TEventArgs). </li></ul><ul><ul><li>Delegate which takes a sender object and a subclass of EventArgs. </li></ul></ul><ul><ul><li>Allows declaration of “standard” events without having to define new delegates. </li></ul></ul><ul><ul><li>Example… </li></ul></ul>Copyright © 2007 by Martin L. Shoemaker
  41. 41. The Cool Generics: Miscellaneous <ul><li>Nullable </li></ul><ul><li>ArraySegment </li></ul>Copyright © 2007 by Martin L. Shoemaker
  42. 42. Generics and Serialization <ul><li>The bad news: XML serialization doesn’t support generics. </li></ul><ul><li>The good news: Binary serialization does. </li></ul>Copyright © 2007 by Martin L. Shoemaker
  43. 43. Conclusion <ul><li>Generics define classes, methods, interfaces, delegates, and events independent of the types they operate on. </li></ul><ul><li>Types can be constrained. </li></ul><ul><li>.NET Framework provides a number of useful generics for collections and more. </li></ul>Copyright © 2007 by Martin L. Shoemaker
  44. 44. Questions... <ul><li>[email_address] </li></ul>Copyright © 2007 by Martin L. Shoemaker
  45. 45. Everything You Never Wanted to Know About .NET Generics (But Were Afraid Someone Would Ask) Copyright © 2007 by Martin L. Shoemaker

×