Skip to content

Day 02 Generics Deferred enumerables and LINQ

Kobi Hari edited this page Sep 17, 2020 · 1 revision

Day 02 - Generics, Deferred Enumerables and LINQ

Projects:

Fun With Generics Touching the hard points of .net Generics: Constraints, Covariance and Inference
Intro to LINQ Introduction to the lazy nature of .net LINQ

Generics

  • The purpose of Generics
  • About Boxing and Unboxing
  • Static members of Generic classes
  • Generic Inference
  • Generic Invariance, Covariance and Contravariance
  • Generic Constraints

Equatable Value type

  • We have created a value type for complex numbers: Complex
  • We have implemeneted IEquatable<Complex> to allow it to compare itself to other Complex instances
  • We discussed the Equals that we derive from object and how to override it
  • We talked about how to overload the == operator
    • Always implement the != operator too (the compiler enforces it)
    • Always override the Equals method from object (the compiler warns if you do not)
    • Always override the GetHashCode method too (the compiler warns if you do not) - this is important to allow the object to be used as key in HashSets

Intro to deferred execution in LINQ

  • We have created an iterator method using yield return
  • We have investigated the deferred execution nature of the enumerable and spotted 3 significant points in time:
    • Calling the method itself - creates an instance of the IEnumerable
    • Starting the foreach - creates an instance of the IEnumerator
    • Fetching the next item - runs the iterator method until the next yield return
  • We discussed the 2 ways to terminate an iterator
    • By ending the method
    • By calling yield break
  • We Defined an IEnumerable Operator as
    • A function that has predictable output per input
    • That does not cause side effects
    • That creates a new IEnumerable out of its source enumerable
  • We created a Map operator that accepts a generic delegate and projects a new enumerable out of the source by applying the projection delegate on each item
  • We created a TakeFirst operator that accepts a (possibly) infinite stream and completes after the first few items
  • We discussed how infinite enumerables are even possible in the deferred execution paradigm
  • Finally we talked about the basic most common LINQ operators:
    • Select projection operator
    • Where filtering operator
    • OrderBy and ThenBy sorting operators
    • Zip combining operator

TBC next week