CS313D: ADVANCED PROGRAMMING LANGUAGE Computer Science department Lecture 9: Generics & generic Collections
Lecture Contents 2 Introduction Why generics? Generic methods Overloading generic methods Generic classes Generic collections SortedDictionary
Motivation for Generic Methods 3 Overloaded methods are often used to perform similar operations on different types of data. Example: Write a set of overloaded methods to display the elements of an int array, a double array and a char array, respectively.
4
How to use? 5 calls to DisplayAr ray method are identical. The Compiler matches the method based on its segnature
Generic Method 6 T Any type Type checking Consistent processing
7
8 Tip!!
Type Constraints 9 We can restrict the types that can be used with a generic method or class to ensure that they meet certain requirements known as a type constraint C# provides several kinds of type constraints. A class constraint indicates that the type argument must be an object of a specific base class or one of its subclasses. An interface constraint indicates that the type argument s class must implement a specific interface.
IComparable<T> Interface 10 It s possible to compare two objects of the same type if that type implements the generic interface IComparable<T> all IComparable<T> objects are guaranteed to have a CompareTo(object) IComparable<T> objects can be used with the sorting and searching methods of classes in the System.Collections.Generic not all types implement interface IComparable<T> the structures in the Framework Class Library that correspond to the simple types all implement this interface
Finding Maximum 11 an interface constraint: requires each of the method s arguments to be of type IComparab le<t>
12
Overloading Generic Methods 13 A generic method may be overloaded. Each overloaded method must have a unique signature. A class can provide two or more generic methods with the same name but different method parameters. A generic method can be overloaded by nongeneric methods with the same method name. When the compiler encounters a method call, it searches for the method declaration that best matches the method name and the argument types specified in the call.
Generic Classes 14 With a generic class, you can use a simple, concise notation to indicate the actual type(s) that should be used in place of the class s type parameter(s). At compilation time, the compiler ensures your code s type safety, and the runtime system replaces type parameters with type arguments to enable your client code to interact with the generic class.
Example 15 generic Stack class, (e.g., Stack of double, Stack of int, Stack of char, Stack of Employee
16
17
18
Stack app 19
20
21
22
23
24
Generic Collections 25 Namespace System.Collections contains collections that store references to objects. Most new apps should use the collections in the System.Collections.Generic namespace, which contains generic classes such as the List<T> and Dictionary<K, V> classes you learned previously to store collections of specific types. dr. Amal Khalifa, Spr 17
26 Generic collections dr. Amal Khalifa, Spr 17
Introduction 27 A dictionary is the general term for a collection of key value pairs. A hash table is one way to implement a dictionary. The.NET Framework provides several implementations of dictionaries, both generic and nongeneric, all of which implement the IDictionary interface dr. Amal Khalifa, Spr 17
Generic Class SortedDictionary 28 The entries in SortedDictionary are sorted in the tree by key. When the key implements generic interface IComparable<T>, the SortedDictionary uses the results of IComparable<T> method CompareTo to sort the keys. use the same public methods, properties and indexers with classes Hashtable dr. Amal Khalifa, Spr 17
Example 29 Find the number of word occurrences in a line of text. dr. Amal Khalifa, Spr 17
Example 30 does not use a hash table, but instead stores its key value pairs in a binary search tree. dr. Amal Khalifa, Spr 17
31 dr. Amal Khalifa, Spr 17
32 dr. Amal Khalifa, Spr 17
33 Tip dr. Amal Khalifa, Spr 17
34 Text Book Chapter 20: 20.1 20.6 Chapter 21: 21.5
Case Study 35