SlideShare uma empresa Scribd logo
1 de 107
Generics


Learn More @ http://www.learnnowonline.com
   Copyright Ā© by Application Developers Training Company
Objectives




        Learn More @ http://www.learnnowonline.com
             Copyright Ā© by Application Developers Training Company
Objectives
ā€¢ Learn how to create generic classes and
  methods




            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




           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




            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


            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




         Learn More @ http://www.learnnowonline.com
            Copyright Ā© by Application Developers Training Company
Agenda
ā€¢ Introducing Generics




          Learn More @ http://www.learnnowonline.com
             Copyright Ā© by Application Developers Training Company
Agenda
ā€¢ Introducing Generics
ā€¢ Generics and Arrays




          Learn More @ http://www.learnnowonline.com
             Copyright Ā© by Application Developers Training Company
Agenda
ā€¢ Introducing Generics
ā€¢ Generics and Arrays
ā€¢ Generic Interfaces




          Learn More @ http://www.learnnowonline.com
             Copyright Ā© by Application Developers Training Company
Agenda
ā€¢   Introducing Generics
ā€¢   Generics and Arrays
ā€¢   Generic Interfaces
ā€¢   Generic Constraints




            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




        Learn More @ http://www.learnnowonline.com
           Copyright Ā© by Application Developers Training Company
Introducing Generics
ā€¢ You should strive for code reuse




            Learn More @ http://www.learnnowonline.com
               Copyright Ā© by Application Developers Training Company
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
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
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
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
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
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
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
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
Generic Methods




       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




            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




            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




            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




             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



             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




        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




            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




            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




        Learn More @ http://www.learnnowonline.com
           Copyright Ā© by Application Developers Training Company
Advantages of Generics
ā€¢ Type-safety




           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




            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




            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




             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




              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




              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


              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




         Learn More @ http://www.learnnowonline.com
            Copyright Ā© by Application Developers Training Company
Agenda
ā€¢ Introducing Generics




           Learn More @ http://www.learnnowonline.com
              Copyright Ā© by Application Developers Training Company
Agenda
ā€¢ Introducing Generics
ā€¢ Generics and Arrays




          Learn More @ http://www.learnnowonline.com
             Copyright Ā© by Application Developers Training Company
Agenda
ā€¢ Introducing Generics
ā€¢ Generics and Arrays
ā€¢ Generic Interfaces




          Learn More @ http://www.learnnowonline.com
             Copyright Ā© by Application Developers Training Company
Agenda
ā€¢   Introducing Generics
ā€¢   Generics and Arrays
ā€¢   Generic Interfaces
ā€¢   Generic Constraints




            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




        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




            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




            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




        Learn More @ http://www.learnnowonline.com
           Copyright Ā© by Application Developers Training Company
Sorting Arrays
ā€¢ You can easily sort an array of simple data types




            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 canā€™t sort an array of class
  instances




            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 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
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
Sorting with the IComparer Interface




         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




           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




        Learn More @ http://www.learnnowonline.com
           Copyright Ā© by Application Developers Training Company
Sorting with Generic Comparisons
ā€¢ Create a method that compares




           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




            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




            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




            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




        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




           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




            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




            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




         Learn More @ http://www.learnnowonline.com
            Copyright Ā© by Application Developers Training Company
Agenda
ā€¢ Introducing Generics




           Learn More @ http://www.learnnowonline.com
              Copyright Ā© by Application Developers Training Company
Agenda
ā€¢ Introducing Generics
ā€¢ Generics and Arrays




           Learn More @ http://www.learnnowonline.com
              Copyright Ā© by Application Developers Training Company
Agenda
ā€¢ Introducing Generics
ā€¢ Generics and Arrays
ā€¢ Generic Interfaces




           Learn More @ http://www.learnnowonline.com
              Copyright Ā© by Application Developers Training Company
Agenda
ā€¢   Introducing Generics
ā€¢   Generics and Arrays
ā€¢   Generic Interfaces
ā€¢   Generic Constraints




             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




        Learn More @ http://www.learnnowonline.com
           Copyright Ā© by Application Developers Training Company
Generic Interfaces
ā€¢ Implement IComparable in your custom
  classes




          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




            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




         Learn More @ http://www.learnnowonline.com
            Copyright Ā© by Application Developers Training Company
Agenda
ā€¢ Introducing Generics




           Learn More @ http://www.learnnowonline.com
              Copyright Ā© by Application Developers Training Company
Agenda
ā€¢ Introducing Generics
ā€¢ Generics and Arrays




           Learn More @ http://www.learnnowonline.com
              Copyright Ā© by Application Developers Training Company
Agenda
ā€¢ Introducing Generics
ā€¢ Generics and Arrays
ā€¢ Generic Interfaces




           Learn More @ http://www.learnnowonline.com
              Copyright Ā© by Application Developers Training Company
Agenda
ā€¢   Introducing Generics
ā€¢   Generics and Arrays
ā€¢   Generic Interfaces
ā€¢   Generic Constraints




             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




        Learn More @ http://www.learnnowonline.com
           Copyright Ā© by Application Developers Training Company
Generic Constraints
ā€¢ Use generic constraints to ensure that
  parameters meet conditions




            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




         Learn More @ http://www.learnnowonline.com
            Copyright Ā© by Application Developers Training Company
Agenda
ā€¢ Introducing Generics




           Learn More @ http://www.learnnowonline.com
              Copyright Ā© by Application Developers Training Company
Agenda
ā€¢ Introducing Generics
ā€¢ Generics and Arrays




           Learn More @ http://www.learnnowonline.com
              Copyright Ā© by Application Developers Training Company
Agenda
ā€¢ Introducing Generics
ā€¢ Generics and Arrays
ā€¢ Generic Interfaces




           Learn More @ http://www.learnnowonline.com
              Copyright Ā© by Application Developers Training Company
Agenda
ā€¢   Introducing Generics
ā€¢   Generics and Arrays
ā€¢   Generic Interfaces
ā€¢   Generic Constraints




             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




        Learn More @ http://www.learnnowonline.com
           Copyright Ā© by Application Developers Training Company
Generics and Lists
ā€¢ Classes can account for one-to-many
  relationships




           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




           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




           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




           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




             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


             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!




       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

Mais conteĆŗdo relacionado

Semelhante a Generics

Using The .NET Framework
Using The .NET FrameworkUsing The .NET Framework
Using The .NET FrameworkLearnNowOnline
Ā 
JavaScript: Operators and Expressions
JavaScript: Operators and ExpressionsJavaScript: Operators and Expressions
JavaScript: Operators and ExpressionsLearnNowOnline
Ā 
Asynchronous Programming
Asynchronous ProgrammingAsynchronous Programming
Asynchronous ProgrammingLearnNowOnline
Ā 
JavaScript: Values, Types and Variables
JavaScript: Values, Types and VariablesJavaScript: Values, Types and Variables
JavaScript: Values, Types and VariablesLearnNowOnline
Ā 
Building Large Sustainable Apps
Building Large Sustainable AppsBuilding Large Sustainable Apps
Building Large Sustainable AppsBuğra Oral
Ā 
Building Windows 8 Metro Style Applications Using JavaScript and HTML5
Building Windows 8 Metro Style Applications Using JavaScript and HTML5Building Windows 8 Metro Style Applications Using JavaScript and HTML5
Building Windows 8 Metro Style Applications Using JavaScript and HTML5LearnNowOnline
Ā 
What's new in Silverlight 5
What's new in Silverlight 5What's new in Silverlight 5
What's new in Silverlight 5LearnNowOnline
Ā 
New in the Visual Studio 2012 IDE
New in the Visual Studio 2012 IDENew in the Visual Studio 2012 IDE
New in the Visual Studio 2012 IDELearnNowOnline
Ā 
SharePoint Document Management
SharePoint Document ManagementSharePoint Document Management
SharePoint Document ManagementLearnNowOnline
Ā 
KnockOutJS with ASP.NET MVC
KnockOutJS with ASP.NET MVCKnockOutJS with ASP.NET MVC
KnockOutJS with ASP.NET MVCLearnNowOnline
Ā 
Managing site collections
Managing site collectionsManaging site collections
Managing site collectionsLearnNowOnline
Ā 
Mastering Test Automation: How to Use Selenium Successfully
Mastering Test Automation: How to Use Selenium Successfully Mastering Test Automation: How to Use Selenium Successfully
Mastering Test Automation: How to Use Selenium Successfully Applitools
Ā 
Web API HTTP Pipeline
Web API HTTP PipelineWeb API HTTP Pipeline
Web API HTTP PipelineLearnNowOnline
Ā 
SQL Server: Security
SQL Server: SecuritySQL Server: Security
SQL Server: SecurityLearnNowOnline
Ā 
Introducing the Entity Framework
Introducing the Entity FrameworkIntroducing the Entity Framework
Introducing the Entity FrameworkLearnNowOnline
Ā 
Windows 8: Shapes and Geometries
Windows 8: Shapes and GeometriesWindows 8: Shapes and Geometries
Windows 8: Shapes and GeometriesLearnNowOnline
Ā 
Attributes, reflection, and dynamic programming
Attributes, reflection, and dynamic programmingAttributes, reflection, and dynamic programming
Attributes, reflection, and dynamic programmingLearnNowOnline
Ā 
Part of the DLM story: Get your Database under Source Control - SQL In The City
Part of the DLM story: Get your Database under Source Control - SQL In The City Part of the DLM story: Get your Database under Source Control - SQL In The City
Part of the DLM story: Get your Database under Source Control - SQL In The City Red Gate Software
Ā 

Semelhante a Generics (20)

Using The .NET Framework
Using The .NET FrameworkUsing The .NET Framework
Using The .NET Framework
Ā 
JavaScript: Operators and Expressions
JavaScript: Operators and ExpressionsJavaScript: Operators and Expressions
JavaScript: Operators and Expressions
Ā 
Asynchronous Programming
Asynchronous ProgrammingAsynchronous Programming
Asynchronous Programming
Ā 
JavaScript: Values, Types and Variables
JavaScript: Values, Types and VariablesJavaScript: Values, Types and Variables
JavaScript: Values, Types and Variables
Ā 
Building Large Sustainable Apps
Building Large Sustainable AppsBuilding Large Sustainable Apps
Building Large Sustainable Apps
Ā 
Building Windows 8 Metro Style Applications Using JavaScript and HTML5
Building Windows 8 Metro Style Applications Using JavaScript and HTML5Building Windows 8 Metro Style Applications Using JavaScript and HTML5
Building Windows 8 Metro Style Applications Using JavaScript and HTML5
Ā 
What's new in Silverlight 5
What's new in Silverlight 5What's new in Silverlight 5
What's new in Silverlight 5
Ā 
New in the Visual Studio 2012 IDE
New in the Visual Studio 2012 IDENew in the Visual Studio 2012 IDE
New in the Visual Studio 2012 IDE
Ā 
SearchLab
SearchLabSearchLab
SearchLab
Ā 
SharePoint Document Management
SharePoint Document ManagementSharePoint Document Management
SharePoint Document Management
Ā 
KnockOutJS with ASP.NET MVC
KnockOutJS with ASP.NET MVCKnockOutJS with ASP.NET MVC
KnockOutJS with ASP.NET MVC
Ā 
Managing site collections
Managing site collectionsManaging site collections
Managing site collections
Ā 
Web API Basics
Web API BasicsWeb API Basics
Web API Basics
Ā 
Mastering Test Automation: How to Use Selenium Successfully
Mastering Test Automation: How to Use Selenium Successfully Mastering Test Automation: How to Use Selenium Successfully
Mastering Test Automation: How to Use Selenium Successfully
Ā 
Web API HTTP Pipeline
Web API HTTP PipelineWeb API HTTP Pipeline
Web API HTTP Pipeline
Ā 
SQL Server: Security
SQL Server: SecuritySQL Server: Security
SQL Server: Security
Ā 
Introducing the Entity Framework
Introducing the Entity FrameworkIntroducing the Entity Framework
Introducing the Entity Framework
Ā 
Windows 8: Shapes and Geometries
Windows 8: Shapes and GeometriesWindows 8: Shapes and Geometries
Windows 8: Shapes and Geometries
Ā 
Attributes, reflection, and dynamic programming
Attributes, reflection, and dynamic programmingAttributes, reflection, and dynamic programming
Attributes, reflection, and dynamic programming
Ā 
Part of the DLM story: Get your Database under Source Control - SQL In The City
Part of the DLM story: Get your Database under Source Control - SQL In The City Part of the DLM story: Get your Database under Source Control - SQL In The City
Part of the DLM story: Get your Database under Source Control - SQL In The City
Ā 

Mais de LearnNowOnline

SQL: Permissions and Data Protection
SQL: Permissions and Data ProtectionSQL: Permissions and Data Protection
SQL: Permissions and Data ProtectionLearnNowOnline
Ā 
WPF: Working with Data
WPF: Working with DataWPF: Working with Data
WPF: Working with DataLearnNowOnline
Ā 
A tour of SQL Server
A tour of SQL ServerA tour of SQL Server
A tour of SQL ServerLearnNowOnline
Ā 
Introducing LINQ
Introducing LINQIntroducing LINQ
Introducing LINQLearnNowOnline
Ā 
SharePoint: Introduction to InfoPath
SharePoint: Introduction to InfoPathSharePoint: Introduction to InfoPath
SharePoint: Introduction to InfoPathLearnNowOnline
Ā 
Sql 2012 development and programming
Sql 2012  development and programmingSql 2012  development and programming
Sql 2012 development and programmingLearnNowOnline
Ā 
Expression Blend Motion & Interaction Design
Expression Blend Motion & Interaction DesignExpression Blend Motion & Interaction Design
Expression Blend Motion & Interaction DesignLearnNowOnline
Ā 
Introduction to ASP.NET MVC
Introduction to ASP.NET MVCIntroduction to ASP.NET MVC
Introduction to ASP.NET MVCLearnNowOnline
Ā 
Working with Controllers and Actions in MVC
Working with Controllers and Actions in MVCWorking with Controllers and Actions in MVC
Working with Controllers and Actions in MVCLearnNowOnline
Ā 
Creating a User Interface
Creating a User InterfaceCreating a User Interface
Creating a User InterfaceLearnNowOnline
Ā 

Mais de LearnNowOnline (10)

SQL: Permissions and Data Protection
SQL: Permissions and Data ProtectionSQL: Permissions and Data Protection
SQL: Permissions and Data Protection
Ā 
WPF: Working with Data
WPF: Working with DataWPF: Working with Data
WPF: Working with Data
Ā 
A tour of SQL Server
A tour of SQL ServerA tour of SQL Server
A tour of SQL Server
Ā 
Introducing LINQ
Introducing LINQIntroducing LINQ
Introducing LINQ
Ā 
SharePoint: Introduction to InfoPath
SharePoint: Introduction to InfoPathSharePoint: Introduction to InfoPath
SharePoint: Introduction to InfoPath
Ā 
Sql 2012 development and programming
Sql 2012  development and programmingSql 2012  development and programming
Sql 2012 development and programming
Ā 
Expression Blend Motion & Interaction Design
Expression Blend Motion & Interaction DesignExpression Blend Motion & Interaction Design
Expression Blend Motion & Interaction Design
Ā 
Introduction to ASP.NET MVC
Introduction to ASP.NET MVCIntroduction to ASP.NET MVC
Introduction to ASP.NET MVC
Ā 
Working with Controllers and Actions in MVC
Working with Controllers and Actions in MVCWorking with Controllers and Actions in MVC
Working with Controllers and Actions in MVC
Ā 
Creating a User Interface
Creating a User InterfaceCreating a User Interface
Creating a User Interface
Ā 

ƚltimo

presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century educationjfdjdjcjdnsjd
Ā 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfOrbitshub
Ā 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...apidays
Ā 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024The Digital Insurer
Ā 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodJuan lago vƔzquez
Ā 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businesspanagenda
Ā 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Victor Rentea
Ā 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2
Ā 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamUiPathCommunity
Ā 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobeapidays
Ā 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...apidays
Ā 
Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..Christopher Logan Kennedy
Ā 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWERMadyBayot
Ā 
Mcleodganj Call Girls šŸ„° 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls šŸ„° 8617370543 Service Offer VIP Hot ModelMcleodganj Call Girls šŸ„° 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls šŸ„° 8617370543 Service Offer VIP Hot ModelDeepika Singh
Ā 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
Ā 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDropbox
Ā 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxRustici Software
Ā 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistandanishmna97
Ā 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...apidays
Ā 

ƚltimo (20)

presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
Ā 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Ā 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Ā 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
Ā 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Ā 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
Ā 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Ā 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering Developers
Ā 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
Ā 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Ā 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Ā 
Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..
Ā 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
Ā 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
Ā 
Mcleodganj Call Girls šŸ„° 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls šŸ„° 8617370543 Service Offer VIP Hot ModelMcleodganj Call Girls šŸ„° 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls šŸ„° 8617370543 Service Offer VIP Hot Model
Ā 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
Ā 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
Ā 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
Ā 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
Ā 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Ā 

Generics

  • 1. 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
  • 9. Agenda ā€¢ Introducing Generics Learn More @ http://www.learnnowonline.com Copyright Ā© by Application Developers Training Company
  • 10. Agenda ā€¢ Introducing Generics ā€¢ Generics and Arrays 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
  • 45. Agenda ā€¢ Introducing Generics Learn More @ http://www.learnnowonline.com Copyright Ā© by Application Developers Training Company
  • 46. Agenda ā€¢ Introducing Generics ā€¢ Generics and Arrays 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
  • 74. Agenda ā€¢ Introducing Generics Learn More @ http://www.learnnowonline.com Copyright Ā© by Application Developers Training Company
  • 75. Agenda ā€¢ Introducing Generics ā€¢ Generics and Arrays 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
  • 84. Agenda ā€¢ Introducing Generics Learn More @ http://www.learnnowonline.com Copyright Ā© by Application Developers Training Company
  • 85. Agenda ā€¢ Introducing Generics ā€¢ Generics and Arrays 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
  • 93. Agenda ā€¢ Introducing Generics Learn More @ http://www.learnnowonline.com Copyright Ā© by Application Developers Training Company
  • 94. Agenda ā€¢ Introducing Generics ā€¢ Generics and Arrays 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 do Editor

  1. \n
  2. \n
  3. \n
  4. \n
  5. \n
  6. \n
  7. \n
  8. \n
  9. \n
  10. \n
  11. \n
  12. \n
  13. \n
  14. \n
  15. \n
  16. \n
  17. \n
  18. \n
  19. \n
  20. \n
  21. \n
  22. \n
  23. \n
  24. \n
  25. \n
  26. \n
  27. \n
  28. \n
  29. \n
  30. \n
  31. \n
  32. \n
  33. \n
  34. \n
  35. \n
  36. \n
  37. \n
  38. \n
  39. \n
  40. \n
  41. \n
  42. \n
  43. \n
  44. \n
  45. \n
  46. \n
  47. \n
  48. \n
  49. \n
  50. \n
  51. \n
  52. \n
  53. \n
  54. \n
  55. \n
  56. \n
  57. \n
  58. \n
  59. \n
  60. \n
  61. \n
  62. \n
  63. \n
  64. \n
  65. \n
  66. \n
  67. \n
  68. \n
  69. \n
  70. \n
  71. \n
  72. \n
  73. \n
  74. \n
  75. \n
  76. \n
  77. \n
  78. \n
  79. \n
  80. \n
  81. \n
  82. \n
  83. \n
  84. \n
  85. \n
  86. \n
  87. \n
  88. DEMO: rest of section\n