Few questions about C# Messengers and delegates

I recently discovered messenger systems such as the Advanced C# Messenger on the wiki and I’m hazy on a few things.

There are a few different takes on the messenger on the wiki and they all split up implementation into two files – Callback.cs and Messenger.cs. Why is this? Callback.cs just contains some prototypes of delegates. I moved those prototypes to the top of Messenger.cs and everything seemed to work fine. Is there something I’m missing here? Also, I then tried replacing these custom delegate types with Action<>. Again, is there anything I’m overlooking by doing this? Everything seemed to work okay.

The other concern is that I’ve read these event systems can potentially foul up garbage collection due to the messenger holding on to references to delegates. Am I correct in assuming that if I always remember to RemoveListener for every AddListener this problem can be avoided?

It’s true that delegates can prevent garbage collection, so you should always clean them up when you’re done with them.

I am not familiar with the particular scripts you are referring to but it sounds like you are on the right track here.

I don’t think it matters much where the delegate definitions are defined as far as the compiler is concerned, that appears to be a matter of personal preference. I don’t really see any benefit to using Action unless you need to capture local state like a closure, but as I said I’m not familiar with these scripts and the use case they address.

this page may be of interest if you ended reading this post ( Comparison of Unity(C#) Messaging / Events Systems ):
http://forum.unity3d.com/threads/173492-Comparison-of-Unity(C-)-Messaging-Events-Systems