[问题] Delegate回传质疑问?

楼主: Gamakatsu (Gama)   2015-11-02 20:53:23
class Program
{
public delegate string MyDelegate();
static void Main(string[] args)
{
MyDelegate myDelegate = MyMethod1;
myDelegate += MyMethod2;
Console.WriteLine(myDelegate());
Console.ReadKey();
}
public static string MyMethod1()
{
Console.WriteLine("Mythod1");
return "Done";
}
public static string MyMethod2()
{
Console.WriteLine("Mythod2");
return "Done";
}
}
===========================================================
以上是目前自己在练习Delegate观念时打的程式码
想请问各位大大这段程式码的结果为何是印出
MyMethod1
MyMethod2
Done
而不是
MyMethod1
MyMethod2
Done
Done
谢谢大家
作者: fo40225   2015-11-02 21:47:00
多点传送委派只会回传最后一个方法的回传值
作者: bantime (景)   2015-11-02 21:47:00
return 似乎只会接到最后一个 总不可能如果回传int结果最后回来变成int[]吧
作者: fo40225   2015-11-02 21:48:00
msdn.microsoft.com/zh-tw/library/system.delegate备注倒数第二段
作者: O187 (187cm)   2015-11-02 22:47:00
你应该用循环去接
作者: testPtt (测试)   2015-11-03 00:22:00
做完一次先输出一次

Links booklink

Contact Us: admin [ a t ] ucptt.com