Events Delegates and Lambdas C Role of Events
Events, Delegates, and Lambdas C#
Role of Events, Delegates, and Event Handlers § Role of Events § Role of Delegates § Role of Event Handlers
Event Handler Event. Args Event Raiser Delegate Events, Delegats, and Event Handlers
Role of Events To pull bints, right?
Event Raiser Events, Delegats, and Event Handlers
What is an Event? § Events are notifications § Play a central role in. NET framework § Provide a way to trigger notifications from end users or from objects. Button Click Event § Events signal the occurrence of an action/notification. § Objects that raise events don’t need to explicitly know the object that will handle the event. (You can have multiple handlers)
Role of Delegates As real as it gets. .
Delegate The Glue Between the Event Raiser and the Event Handler
What is a delegate? § Specialized class often called a Function Pointer. § The glue/pipeline between an event and an event handler. § Based on Multicast. Delegate base class. § Tracks all the listeners, in an Invocation list.
Event Handler Delegates are a Pipeline Event. Args Event
An event is useless without a delegate
Role of Event Handlers Are you listening?
Event. Args Event Handler
What is an Event Handler? § Responsible for receiving and processing data from a delegate. § Normally receives two paramters: § § Sender § Event. Args responsible for encapsulating event data.
Role of Event. Handler Event Handler public void Button. Click(object sender, Event. Args e) { // Handling of button click occurs here }
Basic Delegate Demo Simple demo to show you can define and use delegates.
Custom Delegates Where the fun begins!
Delegates can stand on their own. No need for Event. Handler or Event. Args. You don’t have to use delegates with events.
Creating Delegates § Custom delegates are defined using the keyword delegate.
int Work. Type Handler Method Delegate Creating Delegates Delegate data from point A to point B.
Delegate Base Classes § § Method – name of method where pipeline is dumping data § Target – object instance of where method resides § Multicast. Delegate – a way to hold multiple delegates (multiple pipelines) You cant inherit from Delegate or Multicast. Delegate. You have to use the delegate keyword.
What is a Multicast Delegate? § Can reference more than one delegate function. (just a list of pipelines) § Tracks delegate references using an invocation list. § Delegates in the list are invoked sequentially.
Creating a delegate instance
Invoking a Delegate Instance
Adding to the Invocation List With one call I can instantly notify multiple handlers!
Invocation List Demo
Events Do we even need them?
Events
Defining an Event § Can be defined using the event keyword. Delegate § Friendly wrapper for delegate.
Raising Events § Events are raised by calling the event like a method: § Need to check if there is anything in the Invocation List. If delegate is null behind the scenes you will get an exception. (Checks if any delegate is attached) § You can also just access the delegate directly:
Events Demo Wow
Event. Args The c# best practice for handling events
Creating custom Event. Args class § When custom data needs to be passed the Event. Args class can be extended. § Keeps signature clean. § If lots of data was needed to raised in event it can be encapsulated in inherited Event. Args object.
Using Event. Handler<T> § . NET uses a generic Event. Handler<T> class that can be used instead of a custom delegate:
Lambdas and delegates Anonymous methods
Anonymous methods § Inline method. § Can get messy.
Lambdas Event determines parameter types. Inline Parameters Lambda operator § A lambda is a concise inline method. § Compiler does a lot of magic for us.
Lambdas with custom delegate:
Lambdas Demo Lana. . Lana… Lana. . … what? … Danger zone!
Action<T> and Func<T, TResult> For the boyz. .
Action<T> § The. NET framework provides several different out-of-the-box delegates that can save you a little bit of coding. § Action<T> accepts a parameter and returns no value.
Action<T> § The. NET framework provides several different out-of-the-box delegates that can save you a little bit of coding. § Action<T> accepts a parameter and returns no value.
Func<T, TResult> § Func<T, TResult> accepts a parameter and returns a value of type TResult.
- Slides: 43