what are delegates? How you used then in your project?
Answers were Sorted based on User's Feedback
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 |
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 |
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 |
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 |
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 |
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 |
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 |
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 |
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 |
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 |
What is the use of thread join in c#?
What is escape character in c#?
How do I download a program to my desktop?
What is the difference between virtual and override in c#?
What is the data provider name to connect to access database?
Can derived classes have greater accessibility than their base types?
Classes and structs can be declared as static, is this statement true or false?
If we inherit a class do the private variables also get inherited ?
Explain the difference between object type and dynamic type variables in c#?
Why we can't create the object of abstract class ?
15 Answers Assyst, CTS, HCL, IBM, L&T,
what are the bugs are faced in a project
Distinguish between finally and finalize blocks?