Kurt Nørmark ©
Department of Computer Science, Aalborg University, Denmark
Abstract Previous lecture Next lecture Index References Contents | This lecture is about type parametrization, also known as generics. We discuss both type parameterized classes, structs, methods, and delegates. We start with a motivational part, in which we understand why existing approaches are not good enough. During this part we develop several versions of a class for mathematical sets. This leads to a generic version of class Set in C#. As another example, we generalize the preexisting string class to String<T>, where T is IComparable. This leads, in a natural way, to a discussion of the contrast between IComparable and IComparable<T>. In the last part of the lecture we study generic methods and generic delegates. |
Motivation for Generic Types |
Operations on sets Slide Annotated slide Contents Index References Textbook |
|
|
The classes IntSet and StringSet Slide Annotated slide Contents Index References Textbook |
|
Program: The class IntSet. |
|
Program: A client of IntSet. |
|
Program: Output from the IntSet client program. |
|
Program: The class StringSet. |
|
Program: A client of StringSet. |
|
Program: Output from the IntSet client program. |
|
|
The class ObjectSet Slide Annotated slide Contents Index References Textbook |
|
Program: The class ObjectSet. |
|
Program: A client of ObjectSet - working with sets of Die objects. |
|
Program: Output from the ObjectSet client program. |
|
Program: A client of ObjectSet - working with set of different types. |
|
Program: Output from the the client of ObjectSet - the version that works with sets of different types. |
|
|
Problems Slide Annotated slide Contents Index References Textbook |
|
|
|
|
Generic Types |
The generic class Set<T> Slide Annotated slide Contents Index References Textbook |
|
Program: The class Set<T>. |
|
Program: A client of Set<T> - working with sets of different types. |
|
Program: Output from the Set<T> client program. |
|
Exercise 11.4. Intersection, union, and difference: Operations on sets | On the accompanying slide we have shown a generic class set<T>. Add the classical set operations intersection, union and set difference to the generic class set<T>. Test the new operations from a client program. Hint: The enumerator, that comes with the class set<T>, may be useful for the implementation of the requested set operations. |
Exercise 11.4. An element access operation on sets | The only way to get access to an element from a set is via use of the enumerator (also known as the iterator) of the set. In this exercise we wish to change that. Invent some operation on the set that allows you to take out an existing element in the set. This corresponds to accessing a given item in an array or a list, for instance via an indexer: arr[i] and lst[j]. Notice in this context that there is no order between elements in the set. It is not natural to talk about "the first" or "the last" element in the set. Given the invented operation in Set<T> use it to illustrate that, for some concrete type T, no casting is necessary when elements are accessed from Set<T> |
Exercise 11.4. A generic Pair class | Program a generic class Pair<S,T> which aggregates two values of the types S and T respectively. It is sufficient to program non-mutable pairs. Be sure to equip the class with an appropriate constructor, and appropriate getter properties. |
|
Generic Types Slide Annotated slide Contents Index References Textbook |
|
|
|
Constraints on Formal Type Parameters Slide Annotated slide Contents Index References Textbook |
|
Program: Illustrations of the various constraints on type parameters. |
|
Constraints: Strings of comparable elements Slide Annotated slide Contents Index References Textbook |
|
|
Program: The generic class String<T>. |
|
Program: Illustrating a number of String<int> objects. |
|
Program: Output from the String<int> program. |
|
Program: Illustrating Strings of different types. |
|
Program: Output from the String of different types program. |
|
Exercise 11.5. Comparable Pairs | This exercise is inspired by an example in the book by Hansen and Sestoft: C# Precisely. Program a class ComparablePair<T,U> which implements the interface IComparable<ComparablePair<T,U>>. If you prefer, you can build the class ComparablePair<T,U> on top of class Pair<S,T> from an earlier exercise in this lecture. It is required that T and U are types that implement Icomparable<T> and Icomparable<U> respectively. How is that expressed in the class ComparablePair<T,U>? The generic class ComparablePair<T,U> should represent a pair (t,u) of values/objects where t is of type T and u is of type U. The generic class should have an appropriate constructor that initializes both parts of the pair. In addition, there should be properties that return each of the parts. Finally, the class should - of course - implement the operation CompareTo because it is prescribed by the interface System.IComparable<ComparablePair<T,U>>. Given two pairs p = (a,b) and q= (c,d). p is considered less than q if a is less than c. If a is equal to c then b and d controls the ordering. This is similar to lexicographic ordering on strings. If needed, you may get useful inspiration from the Icomparable class String<T> on the accompanying slide. Be sure to test-drive your solution! |
Another example of constraints Slide Annotated slide Contents Index References Textbook |
|
Program: Two generic classes C and D - what is the problem?. |
|
Program: Two generic classes C and D - with compiler errors. |
|
Program: Two generic classes C and D - with the necessary constraints. |
|
Variance Slide Annotated slide Contents Index References Textbook |
|
Program: Sets of check accounts and bank accounts. |
|
|
|
|
Generic structs Slide Annotated slide Contents Index References Textbook |
|
|
|
Program: A partial reproduction of struct Nullable<T>. |
|
Generic interfaces: IComparable<T> Slide Annotated slide Contents Index References Textbook |
|
Program: A reproduction of the non-generic interface IComparable. |
|
Program: A reproduction of the generic interface IComparable<T>. |
|
Program: A class Die that implements IComparable. |
|
Program: A class Die that implements IComparable<T>. |
|
|
Generic equality interfaces Slide Annotated slide Contents Index References Textbook |
|
Program: A reproduction of the generic interface IEquatable<T>. |
|
Program: A reproduction of the generic interface IEqualityComparer<T>. |
|
|
Generic Classes and Inheritance Slide Annotated slide Contents Index References Textbook |
|
|
|
Program: Possible and impossible subclasses of Set classes. |
|
Generic Methods |
Generic Methods Slide Annotated slide Contents Index References Textbook |
|
Program: The generic method ReportCompare in the generic String programs. |
|
Program: An equivalent program without automatic inference of the actual type parameters. |
|
|
Program: A generic bubble sort program. |
|
Program: Output from the generic bubble sort program. |
|
Generic Delegates Slide Annotated slide Contents Index References Textbook |
|
Program: Generic reprogramming of the program that composes functions. |
|
Program: Output from the Generic reprogramming of the program that composes functions. |
|
Program: An example that involves other types than double. |
|
Program: Output from the example that involves the types double, int, and Die. |
|
Predefined generic delegates Slide Annotated slide Contents Index References |
|
Program: A reproduction of the generic Action and Function delegate types. |
|
Generic types and methods - Pros and Cons Slide Annotated slide Contents Index References Textbook |
|
|
Collected references Contents Index |
|
Chapter 11: Generic Types and Methods
Course home Author home About producing this web Previous lecture (top) Next lecture (top) Previous lecture (bund) Next lecture (bund)
Generated: February 7, 2011, 12:20:20