Page 12 : 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
Variance
A
CheckAccount
is a
BankAccount
But is a
Set<CheckAccount>
a
Set<BankAccount>
?
Sets of check accounts and bank accounts.
Covariance and Contravariance - parameters
Covariance
The set types vary in the same way as the element types
Contravariance
The set types vary in the opposite way as the element types
Invariance
The set types are not affected by the variations of the element types
C# 3.0 uses invariance