Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
Hi All,
I have some confusion on Multicast Delegate(in C#),
 
when the multicast delegate is called, it invokes the
 
method and should the method must void? I can't find
 
any official document.
 
Please give me a hand and thanks a lot.
 
You can check the example the link as below.
 
http://msdn.microsoft.com/zh-cn/library/ms173175.aspx
Posted 23-Apr-11 6:33am
jkhuang647
Edited 23-Apr-11 6:34am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

In general, it's a good practice to use delegates with void return types. Otherwise when you have a multicast delegate the return value is unpredictable because the order of delegate invocation is not guaranteed.
  Permalink  
Comments
Richard MacCutchan at 23-Apr-11 11:38am
   
A nice, clear, simple answer that tells us everything we need to know: +5.
Nishant Sivakumar at 23-Apr-11 11:38am
   
Thanks Richard!
SAKryukov at 23-Apr-11 22:22pm
   
Nishant, this is a prudent advice for those who wants to do just the basics -- my 5.
For crazy people like myself there is an advanced use (I used it for quite a practical purposes).
For this advanced use, return value does make sense.
Please see my answer and code sample.
--SA
ThatsAlok at 18-Dec-13 1:54am
   
better to put parameter as reference, if you want to return values from multicast delegate
Sergey Alexandrovich Kryukov at 18-Dec-13 3:11am
   
Why?! I don't see any benefits of it. Can you explain in detail?
—SA
ThatsAlok at 19-Dec-13 4:52am
   
Here :-
public delegate int delegateReturnInt();
event delegateReturnInt evntdelegateReturnInt;
 
public delegate void delegateReturnIntRef(ref int value);
event delegateReturnIntRef evntdelegateReturnIntRef;
evntdelegateReturnInt += new delegateReturnInt(Form1_evntdelegateReturnInt10);
evntdelegateReturnInt += new delegateReturnInt(Form1_evntdelegateReturnInt20);
 

evntdelegateReturnIntRef += new delegateReturnIntRef(Form1_evntdelegateReturnIntRef);
evntdelegateReturnIntRef += new delegateReturnIntRef(Form1_evntdelegateReturnIntRef2);
 
One Way - by using invocation list !-
var list = evntdelegateReturnInt.GetInvocationList();
foreach (var handler in list)
{
delegateReturnInt method = (delegateReturnInt)handler;
int retValue = method();
//do something with retValue...
}
 
Second way:- not good, however
 
int value = 20;
evntdelegateReturnIntRef.Invoke(ref value);
Sergey Alexandrovich Kryukov at 19-Dec-13 12:18pm
   
First sample looks like you copied it from my answer. In second way, you are not returning anything. I would say this approach is acceptable in some cases, but this is not how a problem is posed — how to deal with multiple return values?
—SA
Sergey Alexandrovich Kryukov at 19-Dec-13 12:18pm
   
First sample looks like you copied it from my answer. In second way, you are not returning anything. I would say this approach is acceptable in some cases, but this is not how a problem is posed — how to deal with multiple return values?
—SA
ThatsAlok at 19-Dec-13 12:21pm
   
agree its similar to you... but frankly i hav'nt copied from you :-)...
Sergey Alexandrovich Kryukov at 19-Dec-13 12:30pm
   
Okay, no problem. I just thought you would have to see my answer before commenting, say, to avoid redundancy...
—SA
ThatsAlok at 19-Dec-13 12:39pm
   
yeah i agree.. i missed that...apologies for any misconduct :-)
Sergey Alexandrovich Kryukov at 19-Dec-13 12:43pm
   
Please, no problem at all. I also often miss a lot and write redundant posts, just trying to make it a bit better...
It's good to see that you understand the issue...
Cheers,
—SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

You can only use return type of the delegate if you do not invoke the delegate, but manually call each handler from the invocation list of the delegate invocation list. Here is the example on how to do it:
 
delegate int NonVoidDelegate(string parameter);
 
//...

 
NonVoidDelegate delegateInstance = delegate(string parameter) { return 1; };
delegateInstance += delegate(string parameter) { return 2; };
delegateInstance += delegate(string parameter) { return 3; };
 
var list = delegateInstance.GetInvocationList();
 
foreach (var handler in list) {
   NonVoidDelegate method = (NonVoidDelegate)handler;
   int retValue = method("some parameter");
   //do something with retValue...
}
 
—SA
  Permalink  
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Maciej Los 315
1 OriginalGriff 233
2 Aajmot Sk 224
3 Richard MacCutchan 220
4 Marcin Kozub 210
0 OriginalGriff 7,853
1 Sergey Alexandrovich Kryukov 7,107
2 DamithSL 5,604
3 Manas Bhardwaj 4,986
4 Maciej Los 4,790


Advertise | Privacy | Mobile
Web02 | 2.8.1411023.1 | Last Updated 19 Dec 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100