why use delegates?
I understand HOW to use delegates, but in what situations would you actually use them? The examples I see in the literature are all in situations where the code could just be written without ever using them.
Page 1 of 1
why use delegates?
#2
Posted 19 March 2009 - 06:17 AM
Hi, I recently made some notes on delegates (in the context of C#). I think my main sources were wikipedia and MSDN:
A delegate is a 'type-safe function pointer', in other words an object which knows how to call a method on another object. They allow methods to be passed as parameters and are an alternative to interfaces.
When to use a delegate
When to use and interface
A delegate is a 'type-safe function pointer', in other words an object which knows how to call a method on another object. They allow methods to be passed as parameters and are an alternative to interfaces.
When to use a delegate
- 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 derised (Abstraction).
- A class may need more than one implementation of the method.
When to use and interface
- 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 types.
- The method being implemented is linked to the type or identity of the class, e.g comparison methods.
Share this topic:
Page 1 of 1

Help


Back to top









