delegates in C# are strongly typed function pointers. The benefit of delegates are that you can specify a function signature in a method and call that method without having to know which underlying method you are really calling which has the same signature as the delegate.
Some examples are creating a thread, i.e. ThreadStart delegate specifies a method with zero parameters and a void return type:
i.e.
Thread t =new Thread(new ThreadStart(MyFunction));
...
void MyFunction()
{
//do some work
}
Other examples are sorting algorithms, where you need to pass in a function to a method that can be used for sorting.
Delegates in C#?
nowhereman1960
Tridex
<code>
public delegate void DelegatePrintHello();
public class _mclass : Form{
public DelegatePrintHello m_DelegatePrintHello;
public _mclass()
{
m_DelegatePrintHello = new m_DelegatePrintHello(this._displayMsg);
}
public void _displayMsg(object sender, EventArgs e)
{
MessageBox.Show("Delegate called me");
}
private void Form_Load(object sender, EventArgs e)
{
m_DelegatePrintHello(this, new EventArgs());
//m_DelegatePrintHello();
}
}
Hopefully you get the jist. This code is untested.
Martin
Abhishek.itb
Hi, Sunil
MSDN is always for you: http://msdn2.microsoft.com/en-us/library/900fyy8e(VS.71).aspx
http://msdn.microsoft.com/library/default.asp url=/library/en-us/csref/html/vcwlkDelegatesTutorial.asp
Since you seem to be a beginer, just leave the multicast and anonymous delegate concept for later reading.
You'd first read, then if you have any questions pls feel free to let me know.
Thank you
Peter Jausovec - MSFT
Hi,
delegates in C# are strongly typed function pointers. The benefit of delegates are that you can specify a function signature in a method and call that method without having to know which underlying method you are really calling which has the same signature as the delegate.
Some examples are creating a thread, i.e. ThreadStart delegate specifies a method with zero parameters and a void return type:
i.e.
Thread t =new Thread(new ThreadStart(MyFunction));
...
void MyFunction()
{
//do some work
}
Other examples are sorting algorithms, where you need to pass in a function to a method that can be used for sorting.
Mark.