Generics

481 views
423 views

Published on

Learn how to create and understand generic classes and methods.

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
481
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
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • DEMO: rest of section\n
  • Generics

    1. 1. GenericsLearn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    2. 2. Objectives Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    3. 3. Objectives• Learn how to create generic classes and methods Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    4. 4. Objectives• Learn how to create generic classes and methods• Understand the advantages and benefits of generics Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    5. 5. Objectives• Learn how to create generic classes and methods• Understand the advantages and benefits of generics• Explore the use of generics to sort and search in arrays Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    6. 6. Objectives• Learn how to create generic classes and methods• Understand the advantages and benefits of generics• Explore the use of generics to sort and search in arrays• See how to use generic interfaces and constraints Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    7. 7. Objectives• Learn how to create generic classes and methods• Understand the advantages and benefits of generics• Explore the use of generics to sort and search in arrays• See how to use generic interfaces and constraints• Explore the generic List class Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    8. 8. Agenda Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    9. 9. Agenda• Introducing Generics Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    10. 10. Agenda• Introducing Generics• Generics and Arrays Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    11. 11. Agenda• Introducing Generics• Generics and Arrays• Generic Interfaces Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    12. 12. Agenda• Introducing Generics• Generics and Arrays• Generic Interfaces• Generic Constraints Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    13. 13. Agenda• Introducing Generics• Generics and Arrays• Generic Interfaces• Generic Constraints• Generics and Lists Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    14. 14. Introducing Generics Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    15. 15. Introducing Generics• You should strive for code reuse Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    16. 16. Introducing Generics• You should strive for code reuse  Don’t write the same code repeatedly Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    17. 17. Introducing Generics• You should strive for code reuse  Don’t write the same code repeatedly o Put the code in a method and call the method repeatedly Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    18. 18. Introducing Generics• You should strive for code reuse  Don’t write the same code repeatedly o Put the code in a method and call the method repeatedly  Don’t define the same methods repeatedly Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    19. 19. Introducing Generics• You should strive for code reuse  Don’t write the same code repeatedly o Put the code in a method and call the method repeatedly  Don’t define the same methods repeatedly o Put the methods in a class and use the class repeatedly Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    20. 20. Introducing Generics• You should strive for code reuse  Don’t write the same code repeatedly o Put the code in a method and call the method repeatedly  Don’t define the same methods repeatedly o Put the methods in a class and use the class repeatedly• Make your code as generic as possible Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    21. 21. Introducing Generics• You should strive for code reuse  Don’t write the same code repeatedly o Put the code in a method and call the method repeatedly  Don’t define the same methods repeatedly o Put the methods in a class and use the class repeatedly• Make your code as generic as possible  Don’t write ten methods to handle ten different types Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    22. 22. Introducing Generics• You should strive for code reuse  Don’t write the same code repeatedly o Put the code in a method and call the method repeatedly  Don’t define the same methods repeatedly o Put the methods in a class and use the class repeatedly• Make your code as generic as possible  Don’t write ten methods to handle ten different types o Write one method that can handle multiple types Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    23. 23. Introducing Generics• You should strive for code reuse  Don’t write the same code repeatedly o Put the code in a method and call the method repeatedly  Don’t define the same methods repeatedly o Put the methods in a class and use the class repeatedly• Make your code as generic as possible  Don’t write ten methods to handle ten different types o Write one method that can handle multiple types  Prior to Visual Studio 2005 you would use overloaded methods or use objects Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    24. 24. Generic Methods Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    25. 25. Generic Methods• Create a generic method that will perform the same task for multiple data types Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    26. 26. Generic Methods• Create a generic method that will perform the same task for multiple data types• Use a type parameter to declare a generic class or method Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    27. 27. Generic Methods• Create a generic method that will perform the same task for multiple data types• Use a type parameter to declare a generic class or method• Made possible with type parameters Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    28. 28. Generic Methods• Create a generic method that will perform the same task for multiple data types• Use a type parameter to declare a generic class or method• Made possible with type parameters  Placeholder for a specific data type Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    29. 29. Generic Methods• Create a generic method that will perform the same task for multiple data types• Use a type parameter to declare a generic class or method• Made possible with type parameters  Placeholder for a specific data type  Calling code decides, at runtime, what type it will pass to the method Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    30. 30. Generic Methods• Create a generic method that will perform the same task for multiple data types• Use a type parameter to declare a generic class or method• Made possible with type parameters  Placeholder for a specific data type  Calling code decides, at runtime, what type it will pass to the method  Prohibits you from mixing data types Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    31. 31. Generic Classes Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    32. 32. Generic Classes• Generic class has similar behavior to a generic method, but at the class instance level Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    33. 33. Generic Classes• Generic class has similar behavior to a generic method, but at the class instance level• Each call to a method of the class must use the same data type Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    34. 34. Generic Classes• Generic class has similar behavior to a generic method, but at the class instance level• Each call to a method of the class must use the same data type• Declare the class with a type parameter but not the methods Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    35. 35. Advantages of Generics Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    36. 36. Advantages of Generics• Type-safety Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    37. 37. Advantages of Generics• Type-safety  Compiler enforces the requirement that you use a specific data type Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    38. 38. Advantages of Generics• Type-safety  Compiler enforces the requirement that you use a specific data type• Better performance Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    39. 39. Advantages of Generics• Type-safety  Compiler enforces the requirement that you use a specific data type• Better performance  No converting from objects to specific types Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    40. 40. Advantages of Generics• Type-safety  Compiler enforces the requirement that you use a specific data type• Better performance  No converting from objects to specific types• Less code Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    41. 41. Advantages of Generics• Type-safety  Compiler enforces the requirement that you use a specific data type• Better performance  No converting from objects to specific types• Less code  Write a generic method once and pass to it any data type Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    42. 42. Advantages of Generics• Type-safety  Compiler enforces the requirement that you use a specific data type• Better performance  No converting from objects to specific types• Less code  Write a generic method once and pass to it any data type• More flexible code Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    43. 43. Advantages of Generics• Type-safety  Compiler enforces the requirement that you use a specific data type• Better performance  No converting from objects to specific types• Less code  Write a generic method once and pass to it any data type• More flexible code  Passing a different data type does not require new code Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    44. 44. Agenda Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    45. 45. Agenda• Introducing Generics Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    46. 46. Agenda• Introducing Generics• Generics and Arrays Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    47. 47. Agenda• Introducing Generics• Generics and Arrays• Generic Interfaces Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    48. 48. Agenda• Introducing Generics• Generics and Arrays• Generic Interfaces• Generic Constraints Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    49. 49. Agenda• Introducing Generics• Generics and Arrays• Generic Interfaces• Generic Constraints• Generics and Lists Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    50. 50. Generics and Arrays Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    51. 51. Generics and Arrays• Arrays are a handy way to store a group of related items Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    52. 52. Generics and Arrays• Arrays are a handy way to store a group of related items  Easily add or remove an item Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    53. 53. Generics and Arrays• Arrays are a handy way to store a group of related items  Easily add or remove an item  Array class provides methods for sorting and searching Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    54. 54. Sorting Arrays Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    55. 55. Sorting Arrays• You can easily sort an array of simple data types Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    56. 56. Sorting Arrays• You can easily sort an array of simple data types• By default, you can’t sort an array of class instances Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    57. 57. Sorting Arrays• You can easily sort an array of simple data types• By default, you can’t sort an array of class instances  To sort an array, items must all be of the same type, and that type must implement IComparable and provide a CompareTo method Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    58. 58. Sorting Arrays• You can easily sort an array of simple data types• By default, you can’t sort an array of class instances  To sort an array, items must all be of the same type, and that type must implement IComparable and provide a CompareTo method  If the type does not implement IComparable, or if an array contains multiple types, the sort fails at runtime Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    59. 59. Sorting with the IComparer Interface Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    60. 60. Sorting with the IComparer Interface• You can pass to Array.Sort an instance of a class that implements IComparer Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    61. 61. Sorting with the IComparer Interface• You can pass to Array.Sort an instance of a class that implements IComparer• You need to provide a method named Compare that compares two instances of the same type Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    62. 62. Sorting with Generic Comparisons Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    63. 63. Sorting with Generic Comparisons• Create a method that compares Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    64. 64. Sorting with Generic Comparisons• Create a method that compares  Accepts two parameters of the same type, matching the type of data stored in the array Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    65. 65. Sorting with Generic Comparisons• Create a method that compares  Accepts two parameters of the same type, matching the type of data stored in the array  Returns an integer value, representing the comparison of the two parameters Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    66. 66. Sorting with Generic Comparisons• Create a method that compares  Accepts two parameters of the same type, matching the type of data stored in the array  Returns an integer value, representing the comparison of the two parameters• Pass the address of the method to Array.Sort Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    67. 67. Sorting with Generic Comparisons• Create a method that compares  Accepts two parameters of the same type, matching the type of data stored in the array  Returns an integer value, representing the comparison of the two parameters• Pass the address of the method to Array.Sort• No need to implement IComparer Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    68. 68. Searching with Generic Predicates Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    69. 69. Searching with Generic Predicates• Array.IndexOf returns an index of first occurrence of a value in an array Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    70. 70. Searching with Generic Predicates• Array.IndexOf returns an index of first occurrence of a value in an array  Works fine for simple data types but not for complex data types Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    71. 71. Searching with Generic Predicates• Array.IndexOf returns an index of first occurrence of a value in an array  Works fine for simple data types but not for complex data types• Create a method that defines a set of criteria and determines whether an object meets those criteria Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    72. 72. Searching with Generic Predicates• Array.IndexOf returns an index of first occurrence of a value in an array  Works fine for simple data types but not for complex data types• Create a method that defines a set of criteria and determines whether an object meets those criteria• Pass the address of the method to Array.FindIndex Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    73. 73. Agenda Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    74. 74. Agenda• Introducing Generics Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    75. 75. Agenda• Introducing Generics• Generics and Arrays Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    76. 76. Agenda• Introducing Generics• Generics and Arrays• Generic Interfaces Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    77. 77. Agenda• Introducing Generics• Generics and Arrays• Generic Interfaces• Generic Constraints Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    78. 78. Agenda• Introducing Generics• Generics and Arrays• Generic Interfaces• Generic Constraints• Generics and Lists Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    79. 79. Generic Interfaces Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    80. 80. Generic Interfaces• Implement IComparable in your custom classes Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    81. 81. Generic Interfaces• Implement IComparable in your custom classes  You can sort the class using default Array.Sort Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    82. 82. Generic Interfaces• Implement IComparable in your custom classes  You can sort the class using default Array.Sort• Use the generic version of IComparable to avoid the boxing and unboxing issues Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    83. 83. Agenda Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    84. 84. Agenda• Introducing Generics Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    85. 85. Agenda• Introducing Generics• Generics and Arrays Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    86. 86. Agenda• Introducing Generics• Generics and Arrays• Generic Interfaces Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    87. 87. Agenda• Introducing Generics• Generics and Arrays• Generic Interfaces• Generic Constraints Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    88. 88. Agenda• Introducing Generics• Generics and Arrays• Generic Interfaces• Generic Constraints• Generics and Lists Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    89. 89. Generic Constraints Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    90. 90. Generic Constraints• Use generic constraints to ensure that parameters meet conditions Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    91. 91. Generic Constraints• Use generic constraints to ensure that parameters meet conditions  e.g., can only pass to a method type that implements IComparable Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    92. 92. Agenda Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    93. 93. Agenda• Introducing Generics Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    94. 94. Agenda• Introducing Generics• Generics and Arrays Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    95. 95. Agenda• Introducing Generics• Generics and Arrays• Generic Interfaces Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    96. 96. Agenda• Introducing Generics• Generics and Arrays• Generic Interfaces• Generic Constraints Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    97. 97. Agenda• Introducing Generics• Generics and Arrays• Generic Interfaces• Generic Constraints• Generics and Lists Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    98. 98. Generics and Lists Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    99. 99. Generics and Lists• Classes can account for one-to-many relationships Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    100. 100. Generics and Lists• Classes can account for one-to-many relationships  Customers have Orders Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    101. 101. Generics and Lists• Classes can account for one-to-many relationships  Customers have Orders• Create an Orders property in Customer class and store instances of the Order class Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    102. 102. Generics and Lists• Classes can account for one-to-many relationships  Customers have Orders• Create an Orders property in Customer class and store instances of the Order class• Can use an ArrayList Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    103. 103. Generics and Lists• Classes can account for one-to-many relationships  Customers have Orders• Create an Orders property in Customer class and store instances of the Order class• Can use an ArrayList  ArrayList stores objects Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    104. 104. Generics and Lists• Classes can account for one-to-many relationships  Customers have Orders• Create an Orders property in Customer class and store instances of the Order class• Can use an ArrayList  ArrayList stores objects• Use generic List class instead Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    105. 105. Generics and Lists• Classes can account for one-to-many relationships  Customers have Orders• Create an Orders property in Customer class and store instances of the Order class• Can use an ArrayList  ArrayList stores objects• Use generic List class instead  Similar capabilities as ArrayList plus type-safety and better performance Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    106. 106. Learn More! Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    107. 107. Learn More!• This is an excerpt from a larger course. Visit www.learnnowonline.com for the full details! Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company

    ×