This week we tackle one of life’s great quesitons, does Jack Bauer give high fives? Also, we go over everything you need to know about delegates, events, callbacks and closures in .NET.
Big thanks to @kappelcodesalot for being the inspiration for this episode!
News and Updates
- Thanks for the reviews HeliumFIlledStamp, kickinbahk, and maksimerg!
- Angular2 built on TypeScript…Wha?
- Update on New Year’s Resolutions – Not so great
- Amaranth rules, and is done already
- Allen’s course is still going on!
- Michael is…uh, next question
- Giving away a copy of Design Patterns – do the thing we said in the show!
- Find Chuck Norris!
High Level Overview
- Type safe function pointers – allow for methods as parameters
- .Net’s solution for Closures, Callbacks, Events, – Strategy + Observers
- The thriving heart of linq
- Expressive and dynamic functionality in a static world
- Allow for easy/controlled extensions – open/closed principle
What are delegates?
- Type safe function pointers – with a built in iterator
- Methods as parameters – Strategy Pattern – Chess Example (Kaspersky, Random, Bobby Fischer)
- Integral part of linq (anonymous methods/lambdas)
- What are handlers? “are nothing more than methods that are invoked through delegates”
- Funcs, Actions (up to 16!!!)
What are events?
- MultiCasting and null checking
- Observer pattern
- Open closed principle
- Return void, EventHandlers
- Object / EventArgs – yuck!
- Examples in the .net framework: onButtonClick, async
Delegates in other languages
- “Higher Order” functions
- “First Class” functions
- function pointers in C/C++ (not type safe!)
- The jQuery revolution!
- Java is weird, single method interfaces and method reference
Delegates under the hood
- At compile time, a subclass of MultiCastDelegate created for each delegate
- 4 methods: Constructor, Invoke, BeginInvoke, EndInvoke
- 3 (private) fields: _target, _methodPtr, _invocationList
- 2 public properties: Target, Method
- When you call yourmethod, the compiler sees generatedClass.Invoke(yourmethod)
- Func and Action – why define new classes? We can just use generics! Function – down with EventHandler
- Read this book: C# in Depth
Resources we Like
- Delegates (C# Programming Guide)
- C# Events, Delegates and Lambdas on pluralsight
- CLR via C#
- C# in Depth
Tips of the Week
- Generic Unit of Work and Repositories
- Reply All Podcast! – Message App, New to the Internet, Swatting, Interview w/ Wiki Grammar Enforcer
PS: RIP Google Reader