4 Replies - 1826 Views - Last Post: 25 May 2011 - 05:17 AM Rate Topic: -----

#1 tsotne1990  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 230
  • Joined: 01-November 10

Delegates

Posted 24 May 2011 - 06:17 AM

I wonder Why do I even need delegate. Simple question, but I'm new to it and after covering the first 2 chapters about delegates I noticed that it just shortens the complete method invocation and that's all. Correct me if I'm wrong please. I can invoke the methods without delegates. AS I see, it just shortens the notations of invocation. Is this it's only usefulness for in the first place? Thanks
Is This A Good Question/Topic? 0
  • +

Replies To: Delegates

#2 lordofduct  Icon User is offline

  • I'm a cheeseburger
  • member icon


Reputation: 2538
  • View blog
  • Posts: 4,641
  • Joined: 24-September 10

Re: Delegates

Posted 24 May 2011 - 06:28 AM

It allows you to reference a function/method of a certain 'shape' (when I say shape I mean specific parameters and return type). They can also hold onto a list of methods allowing you to call multiple ones at the same time.

This way you can store these references and later on call all of them with out needing to know where they are from. This is helpful for things like events or the sort.

Events are obvious scenarios, (and 'event' is actually just a dressed up delegate), but there are other good examples.

For instance say you have a an interpolation animation going on. You have a generic interpolation method 'shape' that the animation is expecting. You can then define multiple functions that define multiple algorithms for interpolating across the animation. Then down the line you can just pass along which method to use.

This post has been edited by lordofduct: 24 May 2011 - 06:33 AM

Was This Post Helpful? 3
  • +
  • -

#3 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5629
  • View blog
  • Posts: 12,077
  • Joined: 02-June 10

Re: Delegates

Posted 24 May 2011 - 06:36 AM

Delegates are a vital part of C#. But as so many good responses to this question have been posted on DIC already it seems redundant to repeat it. This question come up often - every time a new batch of students start class would be my guess.

Delegates from the beginning, tutorial

Delegates, lambdas and expressions tutorial

Generics primer tutoral (with section on delegates)

Intro to tasks tutorial (which requires delegates)

Not to mention a search for "delegates" within the C# forum gets you a lot of times this has been asked and answered.

And if you look at the bottom if this page you will see several links that are related.

View Postlordofduct, on 24 May 2011 - 07:28 AM, said:

It allows you to reference a function/method of a certain 'shape' (when I say shape I mean specific parameters and return type).


I think the term you are grasping for is the method's "signature". The combination of parameters and return type. That's probably how it is referred to in the OP's textbook.

This post has been edited by tlhIn`toq: 24 May 2011 - 06:33 AM

Was This Post Helpful? 4
  • +
  • -

#4 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1253
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: Delegates

Posted 24 May 2011 - 07:23 AM

Those are some pretty nice links Tilq. :D I'll get to reading and practicing, might've missed something before.
Was This Post Helpful? 0
  • +
  • -

#5 DivideByZero  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 238
  • View blog
  • Posts: 551
  • Joined: 02-December 10

Re: Delegates

Posted 25 May 2011 - 05:17 AM

The concept of delegates and events is easy to understand, just understanding how to use them for a specific program gets me sometimes.
Thanks for the links, going to read them now :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1