Page 14 : 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
Generic interfaces: IComparable<T>
We illustrate generic interfaces with
IComparable
A reproduction of the non-generic interface
IComparable
.
A reproduction of the generic interface
IComparable<T>
.
A class Die that implements
IComparable
.
A class Die that implements
IComparable<T>
.
The implementation of the generic interface is more type safe and less clumsy than the implementation of the non-generic solution