0

I am a newbie in c#. I were able to understand the concepts of Delegates like syntax, how to use Delegates. But I still confused about "Why we use delegates". Because It finally used to call a method. Why we go for delegates?

Nikhil Agrawal
  • 47,018
  • 22
  • 121
  • 208
Anees Deen
  • 1,385
  • 2
  • 17
  • 31

7 Answers7

1

We use delegates to be able to call a method at a later time in the execution of the code. We can pass a method to another block of code which can decide if and when to call it.

In this way we can delegate the responsibility for calling a method.

Slugart
  • 4,535
  • 24
  • 32
1

You may consider delegates as a way to "inject" your code with predefined contract in some execution chain.

Dennis
  • 37,026
  • 10
  • 82
  • 150
0

Delegates are used for many things. The most common use if for event handlers. The code that generates the event need know nothing about the code that will handle the event, which can be supplied by a 3rd party. In other words, Microsoft can supply event firing code, leaving you to supply the methods that will handle those events.

Delegates can be used in any situation like this where you provide some sort of generic handler and other code does something specific with it.

Another way to view delegates is that they are the way C# handles the Command Pattern. Rather than having to declare the interfaces and classes often associated with that patetrn in order to call a method, C# lets you pass references to methods around directly, greatly simplifying the way that pattern is implemented in C#.

David Arno
  • 42,717
  • 16
  • 86
  • 131
0

Using a delegate allows the programmer to encapsulate a reference to a method inside a delegate object. The delegate object can then be passed to code which can call the referenced method, without having to know at compile time which method will be invoked.

It is mainly used in event handlers, but that is not it's only usage; See the details of delegates at - http://msdn.microsoft.com/en-us/library/aa288459%28v=vs.71%29.aspx

Kami
  • 19,134
  • 4
  • 51
  • 63
0

You can have a look at this answer as to the use of delegates.

What are the advantages of delegates?

There are multiple Question in this area that you can browse through.

Community
  • 1
  • 1
David Pilkington
  • 13,528
  • 3
  • 41
  • 73
0

Delegates can be sent to another function, in essence making part of the function "customizable".

//both of these calls use the same function (Where) but with a different predicate delegate
var greaterThanZero = intList.Where(e => e > 0);
var lessThanZero = intList.Where(e => e < 0);
David S.
  • 5,965
  • 2
  • 40
  • 77
0

you can imagine a simple example: in your application for export result you have below options 1. export to pdf 2. export tp csv 3. export to txt

All 3 option will have different method implementation, and these method will do SOMETHING with your data.

suppose above three will call below 3 methods respectively: 1. ExportToPdf(Data, filepath.pdf) 2. ExportToCsv(Data, filepath.csv) 3. ExportToTxt(Data, filepath.txt)

As you can see all 3 have same method signature. In future you may have further types of exports also which you don't know as of now. You will use delegate here which will have same method signature, in place of any specific method.

Apart from this, there are other uses of Delegate like, EventHandler, Async method invocation using BeginInvoke.

PKV
  • 773
  • 1
  • 7
  • 17