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);
}
}
Run Code Online (Sandbox Code Playgroud)
谢谢你的回答.
你需要使用Delegate.GetInvocationList:
var results = new List<string>();
foreach (MyDelegate f in del.GetInvocationList()) {
results.Add(f("Beer"));
}
Run Code Online (Sandbox Code Playgroud)
现在,results保存所有返回值.