Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do we need C# delegates

Tags:

c#

delegates

I never seem to understand why we need delegates? I know they are immutable reference types that hold reference of a method but why can't we just call the method directly, instead of calling it via a delegate?

Thanks

like image 349
InfoLearner Avatar asked Nov 26 '10 10:11

InfoLearner


People also ask

Why is C needed?

The C programming language is the recommended language for creating embedded system drivers and applications. The availability of machine-level hardware APIs, as well as the presence of C compilers, dynamic memory allocation, and deterministic resource consumption, make this language the most popular.

Do we need C?

You don't need to know C to create web pages and web applications. But it comes in handy when you want to write an operating system, a program that controls other programs, or a programming utility for kernel development, or when you want to program embedded devices or any systems application.

What is C and why we use C?

C is a general-purpose programming language and can efficiently work on enterprise applications, games, graphics, and applications requiring calculations, etc. C language has a rich library which provides a number of built-in functions. It also offers dynamic memory allocation.


4 Answers

Simple answer: the code needing to perform the action doesn't know the method to call when it's written. You can only call the method directly if you know at compile-time which method to call, right? So if you want to abstract out the idea of "perform action X at the appropriate time" you need some representation of the action, so that the method calling the action doesn't need to know the exact implementation ahead of time.

For example:

  • Enumerable.Select in LINQ can't know the projection you want to use unless you tell it
  • The author of Button didn't know what you want the action to be when the user clicks on it
  • If a new Thread only ever did one thing, it would be pretty boring...

It may help you to think of delegates as being like single-method interfaces, but with a lot of language syntax to make them easy to use, and funky support for asynchronous execution and multicasting.

like image 188
Jon Skeet Avatar answered Oct 20 '22 03:10

Jon Skeet


Of course you can call method directly on the object but consider following scenarios:

  1. You want to call series of method by using single delegate without writing lot of method calls.
  2. You want to implement event based system elegantly.
  3. You want to call two methods same in signature but reside in different classes.
  4. You want to pass method as a parameter.
  5. You don't want to write lot of polymorphic code like in LINQ , you can provide lot of implementation to the Select method.
like image 33
TalentTuner Avatar answered Oct 20 '22 03:10

TalentTuner


Because you may not have the method written yet, or you have designed your class in such a way that a user of it can decide what method (that user wrote) the user wants your class to execute.

They also make certain designs cleaner (for example, instead of a switch statement where you call different methods, you call the delegate passed in) and easier to understand and allow for extending your code without changing it (think OCP).

Delegates are also the basis of the eventing system - writing and registering event handlers without delegates would be much harder than it is with them.

See the different Action and Func delegates in Linq - it would hardly be as useful without them.

Having said that, no one forces you to use delegates.

like image 36
Oded Avatar answered Oct 20 '22 01:10

Oded


  1. Delegates supports Events
  2. Delegates give your program a way to execute methods without having to know precisely what those methods are at compile time
like image 8
Nanda Avatar answered Oct 20 '22 02:10

Nanda