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 does static void Main(string[] args) in C# mean????????

5 Answers   ssinformatics,


I have a very important query in my mind. Please help me regarding this. I don't have any real time exp in .net. But I have a knowledge it .net. I got an offer from an MNC company as a software developer has I had kept 2 years of fake exp. Even though for this job I had worked hard to crack interview for about an year. So, I would like to know how difficult it will be for working in real time as I don't have real time exp. Please tell me as soon as possible bcoz I need to join by next month. Can i sustain over there for a longer time or not. And also let me know how to work pressure will be over there. Please help me regarding this. I'm getting tension thinking about it. Thank you.

1 Answers  


What is c sharp used for?

0 Answers  


What is a string in c#?

0 Answers  


Define an assembly in .net?

0 Answers  






What is the difference between public, static, and void?

0 Answers  


What are satellite assemblies? How will you get the different language strings?

0 Answers  


What do you know about WM_CHAR message?

0 Answers   C DAC,


Is static thread safe?

0 Answers  


Can a class have multiple constructors c#?

0 Answers  


How do you sort a list in c#?

0 Answers  


What is the base class in .net from which all the classes are derived from?

0 Answers  


Categories