generics thomas j. fuchsse.inf.ethz.ch/.../ss2007/251-0290-00/exercises/generics.pdf ·...

22
Generics Thomas J. Fuchs

Upload: others

Post on 14-Mar-2020

8 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Generics Thomas J. Fuchsse.inf.ethz.ch/.../ss2007/251-0290-00/exercises/Generics.pdf · 2007-05-03 · Parametric Polymorphism (Generics) Benefits: àCode reuse àFaster code (no

Generics

Thomas J. Fuchs

Page 2: Generics Thomas J. Fuchsse.inf.ethz.ch/.../ss2007/251-0290-00/exercises/Generics.pdf · 2007-05-03 · Parametric Polymorphism (Generics) Benefits: àCode reuse àFaster code (no

Parametric Polymorphism Parametric Polymorphism (Generics)(Generics)

Benefits:Code reuseFaster code (no runtime casts)Safer programming (static type-checking)World’s first cross-language generics (not just for C#, but C++ and VB and other languages running on the CLR)

For use in:Collection classesAlgorithmsStructured types

Page 3: Generics Thomas J. Fuchsse.inf.ethz.ch/.../ss2007/251-0290-00/exercises/Generics.pdf · 2007-05-03 · Parametric Polymorphism (Generics) Benefits: àCode reuse àFaster code (no

GenericsGenerics

Why generics?Type checking, no boxing, no downcastsIncreased sharing (typed collections)

How are C# generics implemented?Instantiated at run-time, not compile-timeChecked at declaration, not instantiationWork for both reference and value typesExact run-time type information

Page 4: Generics Thomas J. Fuchsse.inf.ethz.ch/.../ss2007/251-0290-00/exercises/Generics.pdf · 2007-05-03 · Parametric Polymorphism (Generics) Benefits: àCode reuse àFaster code (no

Implementation DetailsImplementation Details

Type-checked at declaration, not at use (unlike C++ templates)

After type check, translated to Generic IL bytecode which supports explicit type parameters and validation of generic code

Page 5: Generics Thomas J. Fuchsse.inf.ethz.ch/.../ss2007/251-0290-00/exercises/Generics.pdf · 2007-05-03 · Parametric Polymorphism (Generics) Benefits: àCode reuse àFaster code (no

The Most Simple ExampleThe Most Simple Example

List<T> myList = new List<T>();

Page 6: Generics Thomas J. Fuchsse.inf.ethz.ch/.../ss2007/251-0290-00/exercises/Generics.pdf · 2007-05-03 · Parametric Polymorphism (Generics) Benefits: àCode reuse àFaster code (no

The Most Simple ExampleThe Most Simple Example

List<int> myList = new List<int>();

Page 7: Generics Thomas J. Fuchsse.inf.ethz.ch/.../ss2007/251-0290-00/exercises/Generics.pdf · 2007-05-03 · Parametric Polymorphism (Generics) Benefits: àCode reuse àFaster code (no

Placeholders for Generic TypesPlaceholders for Generic Types

T … typeK … keyV … value

Page 8: Generics Thomas J. Fuchsse.inf.ethz.ch/.../ss2007/251-0290-00/exercises/Generics.pdf · 2007-05-03 · Parametric Polymorphism (Generics) Benefits: àCode reuse àFaster code (no

GenericsGenerics

Collection classes

Collection interfaces

Collection base classes

Utility classes

Reflection

List<T>Dictionary<K,V>SortedDictionary<K,V>Stack<T> (LIFO)Queue<T> (FIFO)

IList<T>IDictionary<K,V>ICollection<T>IEnumerable<T>IEnumerator<T>IComparable<T>IComparer<T>

Collection<T>KeyedCollection<T>ReadOnlyCollection<T>

Nullable<T>EventHandler<T>Comparer<T>

Page 9: Generics Thomas J. Fuchsse.inf.ethz.ch/.../ss2007/251-0290-00/exercises/Generics.pdf · 2007-05-03 · Parametric Polymorphism (Generics) Benefits: àCode reuse àFaster code (no

The The defaultdefault Keyword in Generic Keyword in Generic CodeCodepublic void ResetPoint()

{

xpos = default(T);

ypos = default(T);

}

Numerical values have a default value of 0.Reference types have a default value of null.Fields of structures are set accordingly to 0 or null.

Page 10: Generics Thomas J. Fuchsse.inf.ethz.ch/.../ss2007/251-0290-00/exercises/Generics.pdf · 2007-05-03 · Parametric Polymorphism (Generics) Benefits: àCode reuse àFaster code (no

Constrain Type Parameters Using Constrain Type Parameters Using wherewhere

When a type parameter is not constrained in any way, the generic type is said to be unbound.

Unbound type parameters are assumed to have only the members of System.Object.

Page 11: Generics Thomas J. Fuchsse.inf.ethz.ch/.../ss2007/251-0290-00/exercises/Generics.pdf · 2007-05-03 · Parametric Polymorphism (Generics) Benefits: àCode reuse àFaster code (no

Constraints for Generic Type Constraints for Generic Type ParametersParameterswhere T : struct

where T : class

where T : new()

where T : NameOfBaseClass

where T : NameOfInterface

Page 12: Generics Thomas J. Fuchsse.inf.ethz.ch/.../ss2007/251-0290-00/exercises/Generics.pdf · 2007-05-03 · Parametric Polymorphism (Generics) Benefits: àCode reuse àFaster code (no

IteratorsIterators

Methods that incrementally compute and return a sequence of values

class Program{

static IEnumerable<int> Range(int start, int count) {for (int i = 0; i < count; i++) yield return start + i;

}

static IEnumerable<int> Squares(IEnumerable<int> source) {foreach (int x in source) yield return x * x;

}

static void Main() {foreach (int i in Squares(Range(0, 10))) Console.WriteLine(i);

}}

0149162536496481

Page 13: Generics Thomas J. Fuchsse.inf.ethz.ch/.../ss2007/251-0290-00/exercises/Generics.pdf · 2007-05-03 · Parametric Polymorphism (Generics) Benefits: àCode reuse àFaster code (no

NotesNotes

Can implement more than one iterator

public IEnumerable<T> BottomToTop {get {

for (int i = 0; i < count; i++) {yield return items[i];

}}

}

public IEnumerable<T> TopToBottom {get {

return this;}

}

Page 14: Generics Thomas J. Fuchsse.inf.ethz.ch/.../ss2007/251-0290-00/exercises/Generics.pdf · 2007-05-03 · Parametric Polymorphism (Generics) Benefits: àCode reuse àFaster code (no

Additional FunctionalityAdditional Functionality

Generic Base ClassesCan implement virtual or a abstract methods.Generic InterfacesGeneric Delegates

Page 15: Generics Thomas J. Fuchsse.inf.ethz.ch/.../ss2007/251-0290-00/exercises/Generics.pdf · 2007-05-03 · Parametric Polymorphism (Generics) Benefits: àCode reuse àFaster code (no
Page 16: Generics Thomas J. Fuchsse.inf.ethz.ch/.../ss2007/251-0290-00/exercises/Generics.pdf · 2007-05-03 · Parametric Polymorphism (Generics) Benefits: àCode reuse àFaster code (no
Page 17: Generics Thomas J. Fuchsse.inf.ethz.ch/.../ss2007/251-0290-00/exercises/Generics.pdf · 2007-05-03 · Parametric Polymorphism (Generics) Benefits: àCode reuse àFaster code (no

Slides partly based on a presentation from:

Anders Hejlsberg“C# 2.0 and Future Directions”

Page 18: Generics Thomas J. Fuchsse.inf.ethz.ch/.../ss2007/251-0290-00/exercises/Generics.pdf · 2007-05-03 · Parametric Polymorphism (Generics) Benefits: àCode reuse àFaster code (no

ResourcesResources

Course Homepage:se.inf.ethz.ch/teaching/ss2007/251-0290-00

Exercise Material:www.inf.ethz.ch/personal/thomas.fuchs

Page 19: Generics Thomas J. Fuchsse.inf.ethz.ch/.../ss2007/251-0290-00/exercises/Generics.pdf · 2007-05-03 · Parametric Polymorphism (Generics) Benefits: àCode reuse àFaster code (no

GenericsGenericspublic class List<T>{

private T[] elements;private int count;

public void Add(T element) {if (count == elements.Length) Resize(count * 2);elements[count++] = element;

}

public T this[int index] {get { return elements[index]; }set { elements[index] = value; }

}

public int Count {get { return count; }

}}

Page 20: Generics Thomas J. Fuchsse.inf.ethz.ch/.../ss2007/251-0290-00/exercises/Generics.pdf · 2007-05-03 · Parametric Polymorphism (Generics) Benefits: àCode reuse àFaster code (no

GenericsGenerics

List<int> intList = new List<int>();

intList.Add(1); // No boxingintList.Add(2); // No boxingintList.Add("Three"); // Compile-time error

int i = intList[0]; // No cast required

Page 21: Generics Thomas J. Fuchsse.inf.ethz.ch/.../ss2007/251-0290-00/exercises/Generics.pdf · 2007-05-03 · Parametric Polymorphism (Generics) Benefits: àCode reuse àFaster code (no

PerformancePerformance

0

0.5

1

1.5

2

2.5

3

3.5

4

int double string (length)element type

Quicksort on 1,000,000 elementsTimes in seconds

GenericNon-generic (object)

Generics for C# and .NET

MSR CambridgeCopenhagen

13th May 2003 21

Page 22: Generics Thomas J. Fuchsse.inf.ethz.ch/.../ss2007/251-0290-00/exercises/Generics.pdf · 2007-05-03 · Parametric Polymorphism (Generics) Benefits: àCode reuse àFaster code (no

public class List{

private object[] elements;private int count;

public void Add(object element) {if (count == elements.Length) Resize(count * 2);elements[count++] = element;

}

public object this[int index] {get { return elements[index]; }set { elements[index] = value; }

}

public int Count {get { return count; }

}}

GenericsGenericspublic class List<T>{

private T[] elements;private int count;

public void Add(T element) {if (count == elements.Length) Resize(count * 2);elements[count++] = element;

}

public T this[int index] {get { return elements[index]; }set { elements[index] = value; }

}

public int Count {get { return count; }

}}

List intList = new List();List intList = new List();

intList.Add(1);intList.Add(1);intList.Add(2);intList.Add(2);intList.Add("Three");intList.Add("Three");

int i = (int)intList[0];int i = (int)intList[0];

List intList = new List();List intList = new List();

intList.Add(1);intList.Add(1); // Argument is boxed// Argument is boxedintList.Add(2);intList.Add(2); // Argument is boxed// Argument is boxedintList.Add("Three");intList.Add("Three"); // Should be an error// Should be an error

int i = (int)intList[0];int i = (int)intList[0]; // Cast required// Cast required

List<int> intList = new List<int>();

intList.Add(1); // No boxingintList.Add(2); // No boxingintList.Add("Three"); // Compile-time error

int i = intList[0]; // No cast required