Jump to content


 


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.


Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

Photo

why use delegates?


  • Please log in to reply
1 reply to this topic

#1 BobLewiston

BobLewiston

  • Members
  • 69 posts
  • OFFLINE
  •  
  • Local time:10:01 PM

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.

BC AdBot (Login to Remove)

 


#2 ާyh

ާyh

  • Members
  • 6 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Manchester
  • Local time:03:01 AM

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.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users