Escolar Documentos
Profissional Documentos
Cultura Documentos
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
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
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
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
Agenda
Introducing Generics Generics and Arrays Generic Interfaces Generic Constraints Generics and Lists
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
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
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
Agenda
Introducing Generics Generics and Arrays Generic Interfaces Generic Constraints Generics and Lists
Generic Constraints
Use generic constraints to ensure that parameters meet conditions
e.g., can only pass to a method type that implements IComparable
Agenda
Introducing Generics Generics and Arrays Generic Interfaces Generic Constraints Generics and Lists
Create an Orders property in Customer class and store instances of the Order class Can use an ArrayList
ArrayList stores objects
Learn More!
This is an excerpt from a larger course. Visit www.learnnowonline.com for the full details!