Lambda Expressions
Throughout the previous sections, you have mainly used class-level methods as targets for your delegates and events, such as the ClockTicked
and ClockWakeUp
methods, that were also used in Exercise 3.05:
var clock = new AlarmClock(); clock.Ticked += ClockTicked; clock.WakeUp += ClockWakeUp; static void ClockTicked(object sender, DateTime e) => Console.Write($"{e:t}..."); Â Â Â Â static void ClockWakeUp(object sender, EventArgs e) { Â Â Â Â Console.WriteLine(); Â Â Â Â Console.WriteLine("Wake up"); }
The ClockWakeUp
and ClockTicked
methods are easy to follow and step through. However, by converting them into lambda expression syntax, you can have a more succinct syntax and closer proximity to where they are in code.
Now convert the Ticked
and WakeUp
events to use two different lambda expressions:
clock.Ticked += (sender, e) => { Â Â Â Â Console.Write($"{e...