0
dangling returns w/ delegates multicasting
// it only returns the string of func_B() // what happens with the returned from func_A? delegate string MyDelegate(); MyDelegate d1; MyDelegate d2 = new MyDelegate( func_A ); MyDelegate d3 = new MyDelegate( func_B ); d1 = d2 + d3; WriteLine( d1 ); // its returned to a limbo where the GC destroys it? // ( in C# when return something to nowhere = exception
6 odpowiedzi
+ 4
Kiwwi# I imagine the return of the first delegate gets assigned to a variable, let's call it result.
Then the next delegate is called and it's return value is assigned to the same result variable.
The return value of the first delegate is likely over written by the next delegate return value.
Therefore, the return value of the multicast method will reflect the last delegate that runs.
Think of it as something similar to:
string Run() {
string result = null;
List<delegate> dels = GetDelegates();
foreach(d in dels) {
result = d();
}
return result;
}
+ 4
Typically... I wouldn't use Multicast Delegates with a return type. I was actually surprised to see this as a question. I guess if you can dream it, it can come up. 😉
I'm not familiar with the term, "causal chains" in a programming context. In the general sense, rather than using Multicast Delegates for "method chaining," there are design patterns that might work better.
It just depends on what you are referring to with "causal chaining."
+ 3
Kiwwi# When asking technical questions like this, you can't assume people are aware of everything you're thinking and have full context of what you're referring to.
This question, as it's presented requires a bit of effort to guess what specifically you're trying to figure out.
It would help if you included a link to working code so people can see exactly what you're referring to.
It also helps if you included links to any articles you're reviewing to provide complete context.
The current pseudo code doesn't make it clear what you're expecting.
That said, I put together a version of what I think you're trying to do in this code link:
https://code.sololearn.com/cR85wTs7JN3S/?ref=app
The code output should be self explanatory.
+ 1
David Carroll
you understand it well
the question is what happens with the return of the first method
as told in the // comments
+ 1
David Carroll
literally a domino effect
thx for the advice
0
David Carroll
with that approach there are some scenarios were that kind of return chain can have its utility, i.e causal chains maybe