Page 8 : 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 Types
C# supports generic classes, generic structs, generic interfaces, and generic delegate types
Template
C<T>
is not a type
C<T>
is a template from which a type can be constructed
T
is a
formal type parameter
Constructed type
The type constructed from a template
C<int>
,
C<string>
, and
D<
C<int>
>
int
,
string
, and
C<int>
are
actual type parameters
of
C
and
D
The ability to have generic types is known as
parametric polymorphism