Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The usage of delegate

Tags:

c#

delegates

i saw that delegate is used for custom events. as far example

delegate string FuncRef(string Val);

FuncRef fValue = GetFieldName;

fValue("hello");

what i do here just declare delegate and assign a function name to delegate and call like fValue("hello"); whenever it is required.

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

so please tell me in what kind of scenario delegate usage is required except event handling. please guide me with sample code and simulate a situation where i need to call function through delegate except event handling. please show me some real life scenario where we have to call function through delegate.

like image 656
Mou Avatar asked Jun 16 '11 12:06

Mou


People also ask

What is delegate explain?

1 : to entrust to another delegate authority delegated the task to her assistant. 2 : to appoint as one's representative. intransitive verb. : to assign responsibility or authority a good manager knows how to delegate.

What is an example of a delegate?

An example of delegate is when you tell someone to get your mail for you. The definition of a delegate is a representative authorized to speak or act for others. An example of a delegate is a politician who speaks on behalf of a group of people.

When would you use delegates in C#?

Delegates are mainly used in implementing the call-back methods and events. Delegates can be chained together as two or more methods can be called on a single event. It doesn't care about the class of the object that it references. Delegates can also be used in “anonymous methods” invocation.

How do you call a delegate?

A delegate can be declared using the delegate keyword followed by a function signature, as shown below. The following declares a delegate named MyDelegate . public delegate void MyDelegate(string msg); Above, we have declared a delegate MyDelegate with a void return type and a string parameter.


2 Answers

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

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

instead of

var taxAmount = income * 0.15;

In other words: using a variable to hold a reference to a callable entity (a delegate is exactly that) allows you to write code that can change its behavior depending on the parameters passed to it (the value of the delagate we 're passing in). This means more flexible code.

For examples of code that uses delegates you can look at LINQ (of course), but there's also the "delegates 101" example which is relevant in any language: filtering a list.

like image 64
Jon Avatar answered Nov 15 '22 15:11

Jon


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");
like image 44
Alex F Avatar answered Nov 15 '22 16:11

Alex F