what are delegates? How you used then in your project?

Answers were Sorted based on User's Feedback



what are delegates? How you used then in your project?..

Answer / parmjit

/*

we can think deligate as a hook for a specific type of
function interfaces, to which we can hang functions whose
interfaces match with delegates interface. On a single call
to delegate, all the functions which are hooked to it, will
be called with the same parameters for which delegate was
called.


let's say we have two fuctions, both receive a string value
and prints it back on screen as follows. */

void print_lower(string s)
{
Console.writeline(s.ToLower());
}

void print_upper(string s)
{
Console.writeline(s.ToUpper());
}

//e.g. we can declare a delegate type as

delegate void ToPrint(string s);

//We can create it's instance as :

ToPrint tp;

tp = print_lower; // add a function to delegate
tp += print_upper; // add another function to delegate
tp += print_upper; // add one more function to delegate
tp += print_lower; // add one more function to delegate


//Now when we call

tp("Abcd");

/*it will print the following text on console screen


abcd
ABCD
ABCD
abcd

against a single call to delegate instance, all the
functions which were assined or added to it are called.

*/

Is This Answer Correct ?    10 Yes 0 No

what are delegates? How you used then in your project?..

Answer / kiran kumar reddy

A delegate can be defined as a type safe function pointer.
It encapsulates the memory address of a function in your
code. Whenever you create or use an event in code, you are
using a delegate. When the event is thrown, the framework
examines the delegate behind the event and then calls the
function that the delegate points to. delegates can be
combined to form groups of functions that can be called
together.

Is This Answer Correct ?    8 Yes 0 No

what are delegates? How you used then in your project?..

Answer / muhammad usman

Delegate is a type which holds the method(s) referance in
an object, it is also refered as type safe funcation.
Like Pointer fucation in C++

Is This Answer Correct ?    5 Yes 0 No

what are delegates? How you used then in your project?..

Answer / pankaj

Delegate means function pointer.
There are two types of de;egate
1-simple delegate
2-multicast delegate
ex---Suppose in a project u r using copy icon in two
application.then u need to use same code in two app which
based on some conditions.Here u can use delegate.

Is This Answer Correct ?    2 Yes 0 No

what are delegates? How you used then in your project?..

Answer / deepti

delegates just like a pointers,its nothing but hold the
address of a variable..
There r two types of delegates:
1) Single Delegates: it calls only one method.
2) Multiple Delegates: it calls multiple methods.

Is This Answer Correct ?    2 Yes 1 No

what are delegates? How you used then in your project?..

Answer / shafi syed

Delegates are nothing but refer to methods. so u call the
method through delegate and pass the method as
parameter...that's it.

Is This Answer Correct ?    1 Yes 0 No

what are delegates? How you used then in your project?..

Answer / prashanth

Delegate means function pointer.
There are two types of delegate
1-simple delegate
2-multicast delegate.

for simple ex:

private void Page_Load(object sender, System.EventArgs e)
{
Label2.Text= DoAction(new Action(Add)).ToString();
Response.Write ("<BR>");
Label3.Text= DoAction(new Action(Sub)).ToString();
//
// // Put user code to initialize the page here
}

static int DoAction(Action action)
{
return action(5,2);
}
public int Add(int n,int m)
{
return n+m;
}
public int Sub(int n,int m)
{
return n-m;
}

Is This Answer Correct ?    0 Yes 0 No

what are delegates? How you used then in your project?..

Answer / mandeep

Delegate is an object that can refer to a method.

When we are creating delegates, we are creating an object that can hold a reference to a method; it necessarily means that a delegate can invoke the method to which it refers. ..

A very good easy to understand article related with this is here http://www.dotnetfunda.com/articles/article1670-what-are-delegates-in-csharp.aspx (plain and simple)

Is This Answer Correct ?    0 Yes 0 No

what are delegates? How you used then in your project?..

Answer / lavanya

Delegates are pointers to functions
we pass the address of a method along with the
parameters that we want call

ex:
public delegate void delg1(string s)

we call this delg1 as
del=delg1(h1);
del("hello")

here h1 is a method

Is This Answer Correct ?    2 Yes 5 No

what are delegates? How you used then in your project?..

Answer / amit dhiman [mcp, mca, bca]

Delegates are pointers to function, they can call a
function, Basically they are used for creating events.
Every event has been created is gonna be used delegates.

Here you need to create Event handlers with event arguments.

Is This Answer Correct ?    0 Yes 3 No

Post New Answer

More C Sharp Interview Questions

What is a decimal c#?

0 Answers  


What is ienumerable t in c#?

0 Answers  


What is a delegate, why should you use it and how do you call it ?

1 Answers  


What is a Managed Code??

0 Answers  


What is the difference between interface and functional interface?

0 Answers  






What is cosole application?

0 Answers  


What?s the difference between the Debug class and Trace class?

2 Answers  


Can abstract class have constructor c#?

0 Answers  


What is verbatim literal in c#?

0 Answers  


What is architecture of your poroject? how i c an say?

2 Answers   Eminosoft, HCL, Phoenix Technologies,


Are c# strings null terminated?

0 Answers  


define ispostback and give some examples

2 Answers  


Categories