13

Click here to load reader

Soapbox Delegates Part 2

Embed Size (px)

DESCRIPTION

delegate

Citation preview

Page 1: Soapbox Delegates Part 2

C# DelegatesPart 2

Page 2: Soapbox Delegates Part 2

Reminder – What is a Delegate

A delegate is a type that defines a method signature. When you instantiate a delegate, you can associate its instance with any method with a compatible signature. You can invoke (or call) the method through the delegate instance. (https://msdn.microsoft.com/en-us/library/ms173171(v=vs.90).aspx)

public delegate int PerformCalculation(int x, int y);

Page 3: Soapbox Delegates Part 2

Reminder – What is a Delegate

Delegates are like C++ function pointers but are type safe. Delegates allow methods to be passed as parameters. Delegates can be used to define callback methods. Delegates can be chained together; for example, multiple methods can be called on a

single event.

Page 4: Soapbox Delegates Part 2

Basic Example

BasicDemo.cs https://msdn.microsoft.com/en-us/library/ms173172(v=vs.90).aspx

Page 5: Soapbox Delegates Part 2

Bookstore Example

BookDemo.cs https://msdn.microsoft.com/en-us/library/aa288459(v=vs.71).aspx

Page 6: Soapbox Delegates Part 2

Events

An event in C# is a way for a class to provide notifications to clients of that class when some interesting thing happens to an object.

The most familiar use for events is in graphical user interfaces; typically, the classes that represent controls in the interface have events that are notified when the user does something to the control (for example, click a button).

Events provide a generally useful way for objects to signal state changes that may be useful to clients of that object.

Events are declared using delegates.

Page 7: Soapbox Delegates Part 2

Events Example

EventDemo1.cs EventDemo2.cs https://msdn.microsoft.com/en-us/library/aa645739(v=vs.71).aspx

Page 8: Soapbox Delegates Part 2

Delegates in the Real-World

There is a software development firm, IWorkForThee Corporation. This company specializes in developing state-of-the-art software libraries aimed at solving computation problems. IWorkForThee Corporation produces closed source components.They sell a lot of software. This means they have a great list of customers.

Now, IWorkForThee Corporation has sold the first version of its new library DoSomethingLibrary. This library is in the form of a DLL. The code of the library looks like this (HeavyWeightWorkPart1.cs)

Page 9: Soapbox Delegates Part 2

Real World Example Continued

New version of the DoSomethingLibrary with some advanced features required A new logging mechanism was demanded, client wants XML logging. Another client was very happy with the original logging mechanism. But they wanted

more features. Do they implement all the loggers needed by all the consumers?

Page 10: Soapbox Delegates Part 2

Real World Example Continued

Problems with implementing the different loggers: XML formats are substantially different and would require huge effort. Release date would surely be missed and other consumers may not be very happy With so many loggers available there has to be a mechanism for the consumer to choose. This

would require a change in the HeavyWeightWorker Class’s structure. Costs would go up and the profits would go down. What happens if the consumers change the format again?

Page 11: Soapbox Delegates Part 2

Real World Example Continued

The solution is to remove all loggers from the library Why? If consumers are not happy with the logging that the library provides and need

custom logging, let them implement the logging Using delegates we can delegate the logging functionality to the consumers This will help us in the separation of concerns The DoSomethingLibrary is extremely good at doing what it does. Custom logging was

getting too heavy for the library. HeavyWeightWorkPart1.cs

Page 12: Soapbox Delegates Part 2

Delegates vs Interfaces (When??)

Use a delegate in the following circumstances: An eventing design pattern is used. It is desirable to encapsulate a static method. The caller has no need to access other properties, methods, or interfaces on the object implementing the

method. Easy composition is desired. A class may need more than one implementation of the method.

Use an interface in the following circumstances: There is a group of related methods that may be called. A class only needs one implementation of the method. The class using the interface will want to cast that interface to other interface or class types. The method being implemented is linked to the type or identity of the class: for example, comparison methods.

Page 13: Soapbox Delegates Part 2

References

https://msdn.microsoft.com/en-us/library/ms173171(v=vs.90).aspx https://msdn.microsoft.com/en-us/library/ms173172(v=vs.90).aspx https://msdn.microsoft.com/en-us/library/aa288459(v=vs.71).aspx https://msdn.microsoft.com/en-us/library/aa645739(v=vs.71).aspx http://www.codeproject.com/Articles/884981/Understanding-Delegates-in-Csharp-for-be

ginners https://msdn.microsoft.com/en-us/library/ms173173(v=vs.90).aspx