-
Notifications
You must be signed in to change notification settings - Fork 1
Day 02 Delegates and Enumerables
Kobi Hari edited this page Oct 15, 2020
·
1 revision
Fun With Delegates | Historical walk through the evolusion of Delegates and Events |
Fun With Variance | Yet another inspection of the concepts of Covariance and Contravariance |
Fun With Enumerables | Historical Walk through the evolution of IEnumartor and IEnumerable to yield return
|
- We talked about the fact that a delegate inludes a function pointer and a reference to the target instance
- We have seen how we declared delegates in .net 1 using the
delegate
keyword. - We have seen how to instantiate a new delegate ".net 1" style
- We have seen how .net 2 allowed us to remove the
new
keyword when creating a new delegate - We have seen how .net 2 allowed us to create anonymous methods
- We briefly touched the concept of "Closure" where the variables in the scope where the function is defined are captured in an auto generated class
- We moved on to .net 3 and used lambda expressions instead of anonymous methods
- Finally we used the .net 4
Action
andFunc
classes instead of declaring a delegate type
- We talked about
Multicast
delegates that allow to chain a collection of delegates into a single instance - We talked about the immutablity of the Delegate instance
- We used the same concept to create
event
- We used 3 interfaces as test case for variance
-
IEnumerable<out T>
(covariance) that allows type parameter that is a subclass of T because it is only used as output from the interface methods -
IComparer<in T>
(contravariance) that allows type parameter that is a superclass of T becuase it is only used as input to the interface methods -
ICollection<T>
(invariance) that allows only the exact type parameter of T because it is used both as input and as output of interface methods
-
- We have seen that in
Action
andFunc
:- delegate parameters are always marked as
<in T>
- delegate return value is always marked as
<out T>
- delegate parameters are always marked as
- We have defined our own interface where we mix all 3 possibilities
interface IFoo<in T1, out T2, T3>
- We have seen how
IEnumerator
was defined in .net 1 and the contract that goes along with it - We have implemented our own
IEnumerable
that serves as a factory of out customIEnmuerator
- We have seen how .net 2 added generic interfaces that inherit from the original ones
- We noticed the
IEnumerator<T>
also implementsIDisposable
to indicate that the enumerator should be disposed - We saw that
foreach
in .net is actually a compiler shortcut to creating an enumerator, looping over it, and disposing it - We used the .net 3 shortcut syntax
yield return
to let the compiler create fullIEnumerator
andIEnumerable
pairs for us - We discuessed the
deffered
nature of the theyield return
enumerators, where the body of the function is not called until a foreach is executed on it. - We saw that the deffered method only runs until the next
yield return
at each iteration.