Reputation: 8461
I think I've got my head round delegates; Now I know typically most 'things' exist to solve a specific problem, but what problem does a delegate solve.
IMO it solves the issue when we want to call a method which exists project we can't reference from another project (otherwise we may be in a situation where both projects are referencing each other which is not allowed). This makes sense to me and I can see the use.
Are there any other times / reasons / benefits?
Upvotes: 0
Views: 86
Reputation: 9394
I use them in winform for example to close the form through a button.
button.Click += delegate{this.Close();};
Upvotes: 1
Reputation: 171178
They exist so that you can put functions into variables and do operations on them. For example Enumerable.Select
takes a delegate that tells it what to do with each element. How would you implement Select
without delegates? You'd have to implement an interface for example but that is clumsy. (Java does it that way and it is not as nice).
Actually, delegates are very much like one-method interfaces.
They also exist for native-code interop so that you can provide a native function pointer to a native method.
Upvotes: 1