Reputation:
I am well versed in C# but I just can't get my head around delegates.
I can see that they would be useful, if you had multiple scenarios and you could attach different methods to a delegate depending on that scenario, but that is the only time I can understand when to use them. Why not just use a function?
Makes sense for events like MouseClick, wait until MouseClick is fired and then go to MouseClick(), but I don't understand it in code. I understand the function within a function for javascript, and I don't find any programming ways to do that in C#, so is delegate the way?
I have read about 3 blogs today about this, and hoping somebody hear would let me know a time that a delegate is 100% needed, and nothing else would do.
Upvotes: 2
Views: 301
Reputation: 4528
Sometimes it helps me to simplify concepts by referencing familiar concepts.
Think of a delegate as a container (see image below):
A function = the wooden block that fits inside this container.
We can't pass functions directly as parameters for instance but we can pass delegates! So basically we put the wooden block (function) into the container (delegate) and can now we can pass it as a parameter and use it when we need to.
The Signature = the lid. This makes sure we can only put a block with the right shape into the container.
Invoking a delegate = taking all the blocks (functions) in the container and "calling" or "invoking" those functions.
A multicast delegate is when we put more than 1 block into the container (so when we invoke a multicast delegate, all the functions inside will be invoked).
An event would simply be a container (delegate) with a lock. This prevents someone from opening the lid an pouring out all the blocks inside.
These abstractions are pulled from my blog, if you want to read in more detail
Simple explanation to delegates and events
Also, once delegates and events make sense, it would be worth looking at the evolution of the delegate to understand how delegates evolved to lambda functions.
I hope this abstraction give a better understanding of delegates, it certainly did help me.
Upvotes: 0
Reputation: 1483
Events do not always occur in reaction to some UI change. Say you had a background thread that needed to complete before another function was called. It can be extremely useful in these types of scenarios, since it allows you to do things like cross-thread notification.
Upvotes: 0
Reputation: 3120
You can use functions instead of delegates, but delegates makes your code cleaner and readable (less lines, for example).
[]'s
Upvotes: -2
Reputation: 1504152
Why not just use a function?
That's basically what a delegate is. You can think of it as a single method interface, if you want. You can create a delegate instance from an existing method (using a method group conversion) or you can use an anonymous function (lambda expression or anonymous method).
I have a couple of articles on delegates:
Hopefully at least one of those will help you... (I suspect the nearest "function within a function" equivalent is a lambda expression, by the way.)
Upvotes: 4
Reputation: 12741
For some of the examples you are looking at I could see why it's confusing why someone would use delegates over just straight functions. In these simple cases you could easily get around using a basic method but delegates really shine in LINQ. Delegates allow complex queries in LINQ while still being relatively easy to write.
Upvotes: 1