14 generics and iterators

Post on 26-Aug-2014

746 views 2 download

Tags:

description

 

Transcript of 14 generics and iterators

Generics & Iterators

tnngo2@gmail.com

Programming in C#

Generics

What is generics?

What is generics? ensure type-safety the compiler will validate types while compiling, and throw an error if you try to assign the wrong type to a variable.

Benefits No Casting, and Boxing Increased Performance Ensured strongly-typed Model Reduced run-time errors

Namespace for Generics System.Collection.Generic

Namespace for Generics

Implementation https://gist.github.com/2499823

Constraints https://gist.github.com/2505556

Inheriting generic classes

Generic Methods virtual override abstract https://gist.github.com/2507425

Generic Interfaces https://gist.github.com/2506990

Generic Interface Constraints https://gist.github.com/2507482

Question? https://gist.github.com/2507914

Iterators

Iterators is a way of navigating through the values in a collection. specify the way values are generated when the foreach statement accesses the elements within a collection.

Benefits Perform Simplified Iteration Return Infinite values Reduce Complexity Save Memory

Implementation https://gist.github.com/2507655

Question? What is the difference between IEnumerator and IEnumerable?

Question? What is the difference between IEnumerator and IEnumerable?

IEnumerable public IEnumerator GetEnumerator(); IEnumerator public object Current; public void Reset(); public bool MoveNext();