Más contenido relacionado Más de LearnNowOnline (11) Generics1. Generics
Learn More @ http://www.learnnowonline.com
Copyright © by Application Developers Training Company
2. Objectives
Learn More @ http://www.learnnowonline.com
Copyright © by Application Developers Training Company
3. Objectives
• Learn how to create generic classes and
methods
Learn More @ http://www.learnnowonline.com
Copyright © by Application Developers Training Company
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. 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. 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. 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. Agenda
Learn More @ http://www.learnnowonline.com
Copyright © by Application Developers Training Company
11. Agenda
• Introducing Generics
• Generics and Arrays
• Generic Interfaces
Learn More @ http://www.learnnowonline.com
Copyright © by Application Developers Training Company
12. Agenda
• Introducing Generics
• Generics and Arrays
• Generic Interfaces
• Generic Constraints
Learn More @ http://www.learnnowonline.com
Copyright © by Application Developers Training Company
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. Introducing Generics
Learn More @ http://www.learnnowonline.com
Copyright © by Application Developers Training Company
15. Introducing Generics
• You should strive for code reuse
Learn More @ http://www.learnnowonline.com
Copyright © by Application Developers Training Company
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. 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. 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. 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. 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. 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. 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. 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. Generic Methods
Learn More @ http://www.learnnowonline.com
Copyright © by Application Developers Training Company
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. 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. 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. 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. 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. 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. Generic Classes
Learn More @ http://www.learnnowonline.com
Copyright © by Application Developers Training Company
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. 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. 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. Advantages of Generics
Learn More @ http://www.learnnowonline.com
Copyright © by Application Developers Training Company
36. Advantages of Generics
• Type-safety
Learn More @ http://www.learnnowonline.com
Copyright © by Application Developers Training Company
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. 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. 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. 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. 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. 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. 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. Agenda
Learn More @ http://www.learnnowonline.com
Copyright © by Application Developers Training Company
47. Agenda
• Introducing Generics
• Generics and Arrays
• Generic Interfaces
Learn More @ http://www.learnnowonline.com
Copyright © by Application Developers Training Company
48. Agenda
• Introducing Generics
• Generics and Arrays
• Generic Interfaces
• Generic Constraints
Learn More @ http://www.learnnowonline.com
Copyright © by Application Developers Training Company
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. Generics and Arrays
Learn More @ http://www.learnnowonline.com
Copyright © by Application Developers Training Company
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. 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. 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. Sorting Arrays
Learn More @ http://www.learnnowonline.com
Copyright © by Application Developers Training Company
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. 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. 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. 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. Sorting with the IComparer Interface
Learn More @ http://www.learnnowonline.com
Copyright © by Application Developers Training Company
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. 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. Sorting with Generic Comparisons
Learn More @ http://www.learnnowonline.com
Copyright © by Application Developers Training Company
63. Sorting with Generic Comparisons
• Create a method that compares
Learn More @ http://www.learnnowonline.com
Copyright © by Application Developers Training Company
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. 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. 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. 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. Searching with Generic Predicates
Learn More @ http://www.learnnowonline.com
Copyright © by Application Developers Training Company
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. 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. 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. 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. Agenda
Learn More @ http://www.learnnowonline.com
Copyright © by Application Developers Training Company
76. Agenda
• Introducing Generics
• Generics and Arrays
• Generic Interfaces
Learn More @ http://www.learnnowonline.com
Copyright © by Application Developers Training Company
77. Agenda
• Introducing Generics
• Generics and Arrays
• Generic Interfaces
• Generic Constraints
Learn More @ http://www.learnnowonline.com
Copyright © by Application Developers Training Company
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. Generic Interfaces
Learn More @ http://www.learnnowonline.com
Copyright © by Application Developers Training Company
80. Generic Interfaces
• Implement IComparable in your custom
classes
Learn More @ http://www.learnnowonline.com
Copyright © by Application Developers Training Company
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. 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. Agenda
Learn More @ http://www.learnnowonline.com
Copyright © by Application Developers Training Company
86. Agenda
• Introducing Generics
• Generics and Arrays
• Generic Interfaces
Learn More @ http://www.learnnowonline.com
Copyright © by Application Developers Training Company
87. Agenda
• Introducing Generics
• Generics and Arrays
• Generic Interfaces
• Generic Constraints
Learn More @ http://www.learnnowonline.com
Copyright © by Application Developers Training Company
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. Generic Constraints
Learn More @ http://www.learnnowonline.com
Copyright © by Application Developers Training Company
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. 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. Agenda
Learn More @ http://www.learnnowonline.com
Copyright © by Application Developers Training Company
95. Agenda
• Introducing Generics
• Generics and Arrays
• Generic Interfaces
Learn More @ http://www.learnnowonline.com
Copyright © by Application Developers Training Company
96. Agenda
• Introducing Generics
• Generics and Arrays
• Generic Interfaces
• Generic Constraints
Learn More @ http://www.learnnowonline.com
Copyright © by Application Developers Training Company
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. Generics and Lists
Learn More @ http://www.learnnowonline.com
Copyright © by Application Developers Training Company
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. 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. 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. 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. 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. 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. 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. Learn More!
Learn More @ http://www.learnnowonline.com
Copyright © by Application Developers Training Company
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
Notas del editor \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