Page 5 : 21
Object-oriented Programming in C#
Generic Types and Methods
* Motivation for Generic Types
Operations on sets
The classes IntSet and StringSet
The class ObjectSet
Problems
* Generic Types
The generic class Set<T>
Generic Types
Constraints on Formal Type Parameters
Constraints: Strings of comparable elements
Another example of constraints
Variance
Generic structs
Generic interfaces: IComparable<T>
Generic equality interfaces
Generic Classes and Inheritance
* Generic Methods
Generic Methods
Generic Delegates
Predefined generic delegates
Generic types and methods - Pros and Cons
Problems
IntSet
,
StringSet
and
ObjectSet
suffer from both programming and type problems
Problems with
IntSet
and
StringSet
Tedious to write both versions:
Copy and paste
programming.
Error prone to maintain both versions
Problems with
ObjectSet
Elements of the set must be downcasted in case we need to use some of their specialized operations
We can create an inhomogeneous set
A set of "apples" and "bananas"
Downcasting
Inhomogeneous sets can also be seen as an advantage