~/talk

Get Func-y: Delegates in .NET

Talkat NDC London 2017 · 2017-01-16..01-20· [dotnet]
links:none on file
slides:none on file

A small WPF tool demonstrates swapping output behavior at runtime by passing methods as parameters: a custom delegate becomes Func<Person,string> for formatting and Action<List<Person]> for list operations, implemented with lambdas and inlined anonymous methods. The app processes seven Person records (average rating 6.4, earliest start 10/17/1975, top rating 9/10) and shows multicasting via += (causing duplicate runs unless cleared), uses null‑conditional invoke to avoid NREs, and works as shown.

AI-written summary · about

  1. 0:00Start
  2. 0:35Basics of Delegates
  3. 3:05Multi Casting
  4. 8:08Invoking the Delegate
  5. 14:49Assigned Delegate
  6. 17:16Open-Closed Principle
  7. 17:33Single Responsibility Principle
  8. 18:26Custom Delegate
  9. 20:44Delete a Custom Delegate
  10. 29:01Create a Custom Delegate
  11. 30:23Nested Generics
  12. 32:21Assign Action
  13. 33:11Lambda Expression
  14. 38:35A Lambda Expression
  15. 39:12Anonymous Delegate
  16. 40:53So Are You Ready I'M Going To Take the Delegate Keyword and Delete It and Then in between the Parameters and the Method Body I'M Going To Add the Equals Greater than Sign Maybe I'M Going To Try To Add the Equals Greater than Sign That's Our Lambda Operator That's It Now that's a Lambda Expression but the Thing Is this Isn't Usually the Way We See Lambda Expressions this Is Way Too Readable Right I Can See the Parameter Is of Type Person and It's Called Inputs That Looks Very Obviously like a Parameter and I'Ve Got My Method Body in Curly Braces Well the Thing about Lambda Spreche Ins Is They Were Designed To Be Very Compact
  17. 41:23That Looks Very Obviously like a Parameter and I'Ve Got My Method Body in Curly Braces Well the Thing about Lambda Spreche Ins Is They Were Designed To Be Very Compact We Got Land Expressions at the Same Time We Got Link and that's When We'Re like Okay We Want these Really Short Station's Statements that We Can Put as Parameters and Methods and Just Have Them all in One Spot Easy To Read without like Blowing Up the Code so There's a Lot of Syntactic Sugar around Lambda Expressions That Allow Us To Shrink this Down
  18. 42:43Another Thing Is that if I Only Have One Parameter I Don't Need Parentheses around It another Thing by Convention Is that because We'Re Trying To Make Things Compact It's Very Common To Use Single Characters for Parameter Names in My Lab Expressions So Instead of Using Input I Might Use P Now When I'M Picking Single Characters I Try To Pick One That Reminds Me of What It Is since this Is a Person Object I'M Going To Use P and that's To Help My Brain another Thing Is if I Only Have a Single a Single Expression That Has a Return Type I Can Add that Has a That's Returning a Value I Can Get Rid of the Curly Braces
  19. 44:21And Again Now I'M Going To Delete the Delegate Keyword and Put in the Equals Greater than Sign I Can Delete the Parameter Type since I'Ve Only Got One Parameter I Can Get Rid of these Parentheses around It by Convention Single Character Parameter Names and I Can Get Rid of the Curly Braces and the Return Keyword That Doesn't Seem Quite As Difficult as It Did Before Does It in Fact once You Get Used to this Now You Can Just Start Typing Land Expressions Directly into this So this First Name To Lowercase Well I Can Say P Goes to P Dot First Name Dot To Lower
  20. 44:38And I Can Get Rid of the Curly Braces and the Return Keyword That Doesn't Seem Quite As Difficult as It Did Before Does It in Fact once You Get Used to this Now You Can Just Start Typing Land Expressions Directly into this So this First Name To Lowercase Well I Can Say P Goes to P Dot First Name Dot To Lower and Down Here for Full Name I Can Say P Goes to in this Case I'M Going To Do some String Interpolation P Dot Last Name Comma P Dot First Name Just like that and Again because I Know You Don't Believe Me I'M Going To Comment Out the Entire Formatters Class We Commented Out Gone so the Only Code That I Have Is the Code That I'M Seeing Right Here
  21. 50:53And When I Invoke It They'Re all Going To Run and It's Probably Doing some Magic Threading on the Background so Everything Runs at the Same Time no It Does Not Do that It Actually Runs the Methods in the Sequence That They'Re Assigned so the Way That I Can Tell by Looking at this Code Is if You Notice I Did Get My Average Rating I Got My Earliest Start Date and Then I Got My Message Box Show Which Is a Modal Dialog Which Means My Application Stops Processing
  22. 51:36What's Happening Is the Methods Are Simply Being Run in the Order That We Assign Them and Things Get Kind Of Interesting in some Other Scenarios but before I Talk about that Just To Show this a Little More Clearly this One That's Using that Message Box I'M Going To Move that Up in this Method so It's Assigned First So Now if We Do that Same Thing What We'Ll See Is that the Message Box Pops Up but because It's Model None of the Other Methods Have Run Yet
  23. 52:11Because Things Get Interesting once We Start Talking about What Happens if You Get an Exception in One of these Methods Well the Default Behavior Is for Example if the First One Gets an Exception the Other Three Will Not Run that's the Default Way That It Works Now There Are Ways around that You Can Handle Exceptions Kind Of Not by Doing the Easy Dot Invoke but There Are Ways that You Can Get into the Method List and You Can Go Through and Loop through It and Run each Individually
  24. 52:36But There Are Ways that You Can Get into the Method List and You Can Go Through and Loop through It and Run each Individually and Run each Individually inside a Try-Catch Block so that if One of Them Fails You Can Still Go Ahead and Run the Other Ones if You Want some Information on that Again Follow the Links That Are on My Website for this Talk and You'Ll See some Articles That I'Ve Written about Exception Handling Using Delegates so that's Something To Keep in Mind So Again As Soon as I Click Okay Now those Other Three Methods Run
  25. 58:10It Would Take Me Two Minutes and I Have One Minute so What We'Re Going To Do Is Just Do a Quick Review of Why Delegates Are Awesome Hopefully I'Ve Convinced You that It's at Least Something To Look in a Little Further and Hopefully You'Re a Little More Comfortable with What They Are and How They Work in Particular Funk and Action Are Amazing You See Them Everywhere in Linc and Quite Honestly Wherever I See a Method That Takes a Funk or in Action as a Parameter I Treat that as a Big Flashing Sign That Says Put Your Lambda Expression Here So I End Up Using Lambda Expressions Quite a Bit and Again I'M Using Them Just As Anonymous Delegates It's Not That Hard It's Really Really Cool and I Love It It Was a Huge Hurdle for Me It Took Me It Took Me like Six Months
~/search
type to search 6,249 conferences, 138,987 speakers and 185,172 talks
~/help
cmdctrl k
search every conference, speaker and series
/
focus the filter search
h
home
c
conferences
s
speakers
e
series
t
light / dark — the status bar button does the same
?
this help
esc
close this, then clear every filter — or just the search, while you are typing in it

Ignored while you are typing. Pages without a filter rail have no search to focus.