Vous êtes sur la page 1sur 20

Generics

Learn More @ http://www.learnnowonline.com


Copyright by Application Developers Training Company

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

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

Introducing Generics
You should strive for code reuse
Dont write the same code repeatedly
o

Put the code in a method and call the method repeatedly Put the methods in a class and use the class repeatedly

Dont define the same methods repeatedly


o

Make your code as generic as possible


Dont 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

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

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

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

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

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

Sorting Arrays
You can easily sort an array of simple data types By default, you cant 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

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

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

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

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

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

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

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

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

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

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

Vous aimerez peut-être aussi