Using Delegate

I saw that the delegate is used for custom events. as an example

delegate string FuncRef(string Val); FuncRef fValue = GetFieldName; fValue("hello"); 

what I am here is simply declaring a delegate and assigning a function name for delegation and calling fValue("hello"); whenever required.

instead of calling GetFieldName() via a delegate, I can call it directly. so I just want to know why I should use a delegate to call a function, where we can directly call a function ... what is the advantage of calling any function through a delegate.

please tell me in which scenario the use of a delegate is required other than event handling. please help me with a code sample and simulate a situation when I need to call a function through a delegate, in addition to handling events. please show me some real life scenario where we have to call a function through a delegate.

+8
c # delegates
source share
8 answers
 delegate string FuncRef(string Val); FuncRef fValue; // class member if (condition1) fValue = GetFieldName1; else if (condition2) fValue = GetFieldName2; else fValue = GetFieldName3; // Another function fValue("hello"); 
+2
source share

The reason you use delegates instead of directly calling a function is the same reason you do

 var taxRate = 0.15; var taxAmount = income * taxRate; 

instead

 var taxAmount = income * 0.15; 

In other words: using a variable to bind to the called object (this is the delegate) allows you to write code that can change its behavior depending on the parameters passed to it (delagate we 'value passing). This means more flexible code.

For examples of code that delegates use, you can look at LINQ (of course), but there is also an example of "delegates 101" that is relevant to any language: list filtering .

+5
source share

Microsoft tutorial code on C # delegates is another interesting use case. In their example, the Bookstore object has a ProcessPaperbackBooks method that accepts a book processing delegate and applies it to each element of the class.

The cool part is that then, if you need to say a method that collects all the ISBNs of all the books in the store, you do not need to change anything in the bookstore class, but only to determine the function that acts on the books and passes this to the method ProcessPaperbackBooks. Delegated goodness will happen, and your new function will be applied to every item in the bookstore. Scanty, no?

http://msdn.microsoft.com/en-us/library/aa288459(v=vs.71).aspx

+1
source share

Basically, anywhere you want to indicate at runtime which function to call.

The Async BeginInvoke / EndInvoke pattern is a great example of this; The callback is set through the delegate.

0
source share

In short: you can use delegates and event members in combination to be able, for example, to assign an event handler to a class that did not know before, the button class knows when it should fire the click event and does not know inside, you will attach a handler to it mybutton_Click

0
source share

Linq for objects makes extensive use of delegates:

 myAnimals.Where(animal => animal.CanSwim) 

the parameter provided to the Where method ( animal => animal.CanSwim ) is a lambda expression that translates to a delegate that applies to all myAnimals elements.

0
source share

One place I use delegates in is my input handler (C #, XNA)

I have a public void OnKeyboardPress() delegate that I associate with various functions when certain keys are pressed.

Input is a good example; but not a poll for him (that is, all the updates you go โ€œstill readyโ€? you wait until he calls you), for example, a graphical interface. WPF events are delegates.

0
source share

Delegates are first class objects. IOW you can refer to them, reassign them, etc., Like any other object.

0
source share

All Articles