Generic Lightning Talk

List<T>

List<T>

 

List[T]

 

List(Of t)

public interface List<T> {

    void Add(T item);

 

    T this[int index] { get; set; }

 

    ...

}

public interface List<Apple> {

    void Add(Apple item);

 

    Apple this[int index] { get; set; }

 

    ...

}

List<Apple>

List<Fruit>

var bagOfApples = new List<Apple>();

var bagOfFruit = (List<Fruit>)bagOfApples;

 

bagOfFruit.Add(new Watermelon());

Apple apple = bagOfApples[0];

var bagOfApples = new List<Apple>();

var bagOfFruit = (List<Fruit>)bagOfApples;

 

bagOfFruit.Add(new Watermelon());

Apple apple = bagOfApples[0];

var bagOfFruit = new List<Fruit>();

bagOfFruit.Add(new Watermelon());

 

var bagOfApples = (List<Apple>)bagOfFruit;

Apple apple = bagOfApples[0];

 Enumerable<T>

IEnumerable<T>

Iterable[T]

Enumerable<Apple>

Enumerable<Fruit>

var appleTree = new Tree<Apple>();

 

var fruitTree = (Tree<Fruit>)appleTree;

Fruit fruit = fruitTree.Pick();

 

var pearTree = (Tree<Pear>)fruitTree;

Pear pear = pearTree.Pick();

Covariance

Contravariance

Func<List<Fruit>, Juice> blender = bagOfFruit => ...

Func<List<Apple>, Pie> recipe = bagOfApples => ...

 

var appleBlender = (Func<List<Apple>, Juice>)blender;

var fruitPieRecipe = (Func<List<Fruit>, Pie>)recipe;

 

var fruitRecipe = (Func<List<Fruit>, Drink)blender;

var appleRecipe = (Func<List<Apple>, Food>)recipe;

IEnumerable<out T>

 

 

Func<in T, out TResult>

Iterable[+T]

 

 

Function1[-T1, +R]

(Interlude)

(Straw poll)

covariant

out

+

 

contravariant

in

-

covariant

out

plus

 

contravariant

 in          

minus        

Generic Covariance and Contravariance

By hgcummings

Generic Covariance and Contravariance

  • 89