10

Click here to load reader

VT.NET user group 2014-04-21: Delegation and C# delegates

Embed Size (px)

DESCRIPTION

Slides from my April 2014 presentation to the Vermont .NET user group.

Citation preview

Page 1: VT.NET user group 2014-04-21: Delegation and C# delegates

Back to Basics: DelegatesRob HaleApril 21, 2014

Page 2: VT.NET user group 2014-04-21: Delegation and C# delegates

Goals

● Delegation pattern review● Delegates in C#

Page 3: VT.NET user group 2014-04-21: Delegation and C# delegates

Delegation Design Pattern

Page 4: VT.NET user group 2014-04-21: Delegation and C# delegates

Delegation design pattern - simple

Delegator Delegatee

Page 5: VT.NET user group 2014-04-21: Delegation and C# delegates

Delegation design pattern - slightly more complicated

Delegator IDelegatee

Concrete Delegatee1

Concrete Delegatee2

Page 6: VT.NET user group 2014-04-21: Delegation and C# delegates

Coding time...

Page 7: VT.NET user group 2014-04-21: Delegation and C# delegates

Delegates in C#

Page 8: VT.NET user group 2014-04-21: Delegation and C# delegates

What are Delegates?

● Delegates are a reference type that defines a method signature○ Like C++ function pointers but are type safe○ Unlike method overloading, the return type is part of

the signature● They allow methods to be passed as

parameters● They can be used to define callback

methods● They can be chained together

○ For example, multiple methods can be called on a single event

Page 9: VT.NET user group 2014-04-21: Delegation and C# delegates

Coding Time...

Page 10: VT.NET user group 2014-04-21: Delegation and C# delegates

ResourcesCode from this presentation● https://github.

com/robhalevt/20140421_VTdotNET_DelegationAndDelegates

Other reading● http://msdn.microsoft.com/en-us/library/vstudio/ms173171(v=vs.100).aspx● http://msdn.microsoft.com/en-us/library/orm-9780596516109-03-09.aspx● http://en.wikipedia.org/wiki/Delegation_pattern● http://stackoverflow.com/questions/2044301/what-is-delegate