Presentation is loading. Please wait.

Presentation is loading. Please wait.

Events, Delegates, and Lambdas

Similar presentations


Presentation on theme: "Events, Delegates, and Lambdas"— Presentation transcript:

1 Events, Delegates, and Lambdas
C#

2 Role of Events, Delegates, and Event Handlers
Role of Delegates Role of Event Handlers

3 Events, Delegats, and Event Handlers
EventArgs Events, Delegats, and Event Handlers Event Raiser Delegate

4 Role of Events To pull bints, right?

5 Events, Delegats, and Event Handlers
Event Raiser

6 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. Click Event Button 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)

7 Role of Delegates As real as it gets..

8 The Glue Delegate Between the Event Raiser and the Event Handler

9 What is a delegate? Specialized class often called a Function Pointer.
The glue/pipeline between an event and an event handler. Based on MulticastDelegate base class. Tracks all the listeners, in an Invocation list.

10 Delegates are a Pipeline
Event Handler Delegates are a Pipeline Delegate EventArgs Event

11 An event is useless without a delegate

12 Role of Event Handlers Are you listening?

13 Event Handler EventArgs Event Handler

14 What is an Event Handler?
Responsible for receiving and processing data from a delegate. Normally receives two paramters: Sender EventArgs EventArgs responsible for encapsulating event data.

15 Role of EventHandler Event Handler
public void ButtonClick(object sender, EventArgs e) { // Handling of button click occurs here }

16 Basic Delegate Demo Simple demo to show how you can define and use delegates.

17 Custom Delegates Where the fun begins!

18 Delegates can stand on their own.
No need for EventHandler or EventArgs. You don’t have to use delegates with events.

19 Creating Delegates Custom delegates are defined using the keyword delegate.

20 Creating Delegates int WorkType Handler Method Delegate
Delegate data from point A to point B.

21 Delegate Base Classes Method – name of method where pipeline is dumping data Target – object instance of where method resides MulticastDelegate – a way to hold multiple delegates (multiple pipelines) You cant inherit from Delegate or MulticastDelegate. You have to use the delegate keyword.

22 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.

23 Creating a delegate instance

24 Invoking a Delegate Instance

25 Adding to the Invocation List
With one call I can instantly notify multiple handlers!

26 Invocation List Demo

27 Events Do we even need them?

28 Events

29 Defining an Event Can be defined using the event keyword.
Delegate Friendly wrapper for delegate.

30 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:

31 Events Demo Wow

32 EventArgs The c# best practice for handling events

33 Creating custom EventArgs class
When custom data needs to be passed the EventArgs class can be extended. Keeps signature clean. If lots of data was needed to raised in event it can be encapsulated in inherited EventArgs object.

34 Using EventHandler<T>
.NET uses a generic EventHandler<T> class that can be used instead of a custom delegate:

35 Lambdas and delegates Anonymous methods

36 Anonymous methods Inline method. Can get messy.

37 Event determines parameter types.
Lambdas Event determines parameter types. Inline Parameters Lambda operator A lambda is a concise inline method. Compiler does a lot of magic for us.

38 Lambdas with custom delegate:

39 Lambdas Demo Lana.. Lana… Lana.. … what? … Danger zone!

40 Action<T> and Func<T, TResult>
For the boyz..

41 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.

42 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.

43 Func<T, TResult>
Func<T, TResult> accepts a parameter and returns a value of type TResult.


Download ppt "Events, Delegates, and Lambdas"

Similar presentations


Ads by Google