I have a question about delegate in this code, I add three method to delegate. There're return a string. In the line
string delOut = del("Beer");
to my valuable delOut delegate assignet this "Length : 4"
How can I collect all strings that methods in delegate returns ?
public class NaForum
{
public delegate string MyDelegate(string s);
public void TestDel()
{
MyDelegate del = s => s.ToLower();
del += s => s.ToUpper();
del += s => string.Format("Length : {0}", s.Length);
string delOut = del("Beer");
Console.WriteLine(delOut);
}
}
Thanks for any answers.
You need to use Delegate.GetInvocationList
:
var results = new List<string>();
foreach (MyDelegate f in del.GetInvocationList()) {
results.Add(f("Beer"));
}
Now, results
holds all of the return values.
See C#:Creating Multicast delegate with boolean return type: You need to do the multicasting by yourself to get the individual return values.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With