BleepingComputer.com: why use delegates?

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

why use delegates?

#1 User is offline   BobLewiston 

  • Member
  • PipPip
  • Find Topics
  • Group: Members
  • Posts: 69
  • Joined: 07-November 08

Posted 26 February 2009 - 06:25 PM

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.

#2 User is offline   Þ§yÞhï 

  • New Member
  • Pip
  • Find Topics
  • Group: Members
  • Posts: 6
  • Joined: 18-February 09
  • Gender:Male
  • Location:Manchester

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
  • 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.
E.G IComparable (C#) - a single-method interface instead of a delegate.

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users