Description: This topic discusses the basics of delegates.
Basics
Try to think of delegates in these terms. Although not exact, they can help you wrap your head around them if you're new. If you're not new to delegates, then the more advanced stuff will be near the end of the topic.
- Think of delegates as a container for methods (Sounds like a class. Right? That's a good way to think of them).
- When you use a delegate, you instantiate it, just as you would a class.
- When using a delegate, you can add and remove methods from and to it.
- Now, to use a delegate, you must add methods to it that have the same signature as the delegate (This includes parameter list and return type).
- In reality, think of delegates as holding a reference to a method.
Declaring a Delegate
// <access modifier> delegate <return type> <DelegateName> (parameter list)
public delegate void MyMessageDelegate(string message);
Creating Methods with the same signature as the Delegate Next you will want to create as many methods as you would like that you will possibly call via the Delegate. Don't get wrapped up in this. Just know that these are some methods that you would like to eventually or possibly call
private void MessageSunny(string sunnyMsg)
{
MessageBox.Show("Sunny Message: " + sunnyMsg);
}
private void MessageCloudy(string cloudyMsg);
{
MessageBox.Show("Cloudy Message: " + cloudyMsg);
}
Using the delegate
Now that we have some methods and a delegate (Note: methods and delegates must have same signature), we can use the Delegate.
Add a button to a Windows Form and in the click event, add the following code:
private void btnWeather_Click(object sender, EventArgs e)
{
// <Type (Here is where it is like a class)><YourOjectName> = new <Type>(<Method to raise/call>); MyMessageDelegate MessageObject = new MyMessageDelegate(MessageSunny);
// Add another method to the Object MessageObject += new MyMessageDelegate(MessageCloudy);
MessageObject("Delegate is sending this message!");
}
What does all that above mean?
1. The fiest line creates a new instance of the Delegate the we created. (Notice the similarity between Delegates and Classes)
2. Within the first line, you are creating an object and passing the method into the constructor of the Delegate
3. Remember, I said that you can think of Delegates as containers for methods? Well the Second line is where we are adding another method to the Delegate(Container).
4. The act of assigning muliple methods to the delegate is called multicasting.
5. So the results of the above lines of code give us the following
a. A Delegate Object
b. Two methods within the Delegate Object (MessageSunny and MessageCloudy)
6. The last line is where we are using the Delegate as a method. The parameter we are passing "Delegate is sending this message!", is the parameter that will be sent to all of the methods that have been added to the Delegate.
Executing the Delegate
When the button is clicked in the example above. You will get to MessageBoxes. This happens because the first method (Sunny Message) gets called, and then the second method gets called (Cloudy Message).
Results:
Sunny Message: Delegate is sending this message!
Cloudy Message: Delegate is sending this message!
Removing a method from a Delegate If you need to remove a method from the Delegate Object, you can use the example below
MessageObject -= new MyMessageDelegate(MessageCloudy);
Using a Delegate with a return Type
Suppose we change the example above to return a type. Notice the changes below from the exisiting Delegate and Methods.
// <access modifier> delegate <return type> <DelegateName> (parameter list)
public delegate void string MyMessageDelegate(string message);
private void string MessageSunny(string sunnyMsg)
{
MessageBox.Show(return "Sunny Message: " + sunnyMsg);
}
private void string MessageCloudy(string cloudyMsg);
{
MessageBox.Show(return "Cloudy Message: " + sunnyMsg);
}
To use it, you would do the following: MyMessageDelegate newDelegate = new MyMessageDelegate(MessageSunny); MessageBox.Show(newDelegate("Message from new Delegate");