Jump to content
  • Advertisement
Sign in to follow this  
deadlydog

C# multicast delegates

This topic is 3644 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm using multicast delegates in my app, and I have found that there are two ways to create them. First I define the delegate prototype, so: delegate void DelegatePrototype(string s); then I declare the delegate variable to hold the function pointers: DelegatePrototype MyDelegate = null; Now, here there are two ways I can add functions to the delegate: Method 1 - MyDelegate += new DelegatePrototype(SomeFunction); Method 2 - MyDelegate += SomeFunction; Both ways seem to work, and both ways allow me to remove the function using either: Method 1 - MyDelegate -= new DelegatePrototype(SomeFunction); Method 2 - MyDelegate -= SomeFunction; My question is, is there any difference between using methods 1 and 2 for adding and removing function pointers? I have found one difference. I have a class defined with functions that handle the adding and removing of the function pointers, so it has the functions:
public void AddFunction(DelegatePrototype cFunctionToCall)
{
   MyDelegate += new DelegatePrototype(cFunctionToCall);
}

public void RemoveFunction(DelegatePrototype cFunctionToCall)
{
   MyDelegate -= new DelegatePrototype(cFunctionToCall);
}



then to add and remove a function I would do: MyClass.AddFunction(SomeFunction); MyClass.RemoveFunction(SomeFunction); however, the RemoveFunction does not work; it does not find SomeFunction in the MyDelegate variable to remove. This is true when using remove method 1 and 2 (i.e. with and without the new operator). However, if the AddFunction function adds the function pointer using method 2 (i.e. no new operator), then it is able to find SomeFunction in MyDelegate, and it is removed properly. This makes me inclined to use method 2 all the time (i.e. don't use the new operator ever). However, most of the tutorials I've found online do use method 1 (the new operator method). So I'm just wondering is there some disadvantage or problem to using method 2 over method 1? I would assume that method 2 would be a bit quicker since there is no new operator involved, but that's just a guess and isn't really a big deal anyways. I just want to make sure I'm not breaking something by using method 2 over method 1, since it seems to behave how I would expect it to. Any comments are appreciated. Thanks. [Edited by - deadlydog on June 27, 2008 3:48:14 PM]

Share this post


Link to post
Share on other sites
Advertisement
Method1 and Method2 will do the same thing. It was syntactical sugar added for C# 2.0.

EDIT: Although I'm not entirely sure what you're doing in your code snippet. Creating a delegate around another delegate?

Share this post


Link to post
Share on other sites
Quote:
Original post by Mike.Popoloski
Method1 and Method2 will do the same thing. It was syntactical sugar added for C# 2.0.

EDIT: Although I'm not entirely sure what you're doing in your code snippet. Creating a delegate around another delegate?

To finish off the code snippet so you have a better understanding, this is basically what I'm doing (although this is just an example of setting a string; not the actual purpose I'm using this for):

class CMyClass
{
delegate void DelegatePrototype(string s);
public DelegatePrototype MyDelegate = null;
private string sMyString = "";

public void AddFunction(DelegatePrototype cFunctionToCall)
{
MyDelegate += new DelegatePrototype(cFunctionToCall);
}

public void RemoveFunction(DelegatePrototype cFunctionToCall)
{
MyDelegate -= new DelegatePrototype(cFunctionToCall);
}

public void SetString(string s)
{
MyString = s;
}
}

static void Main()
{
CMyClass MyClass = new CMyClass();

MyClass.AddFunction(MyClass.SetString);
MyClass.MyDelegate("SomeText");
MyClass.RemoveFunction(MyClass.SetString);
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!