// I tried Func method; but it says D() and B() don't return object. // Is there a way to use Delegate method; ? That gives me an eror saying method group is not type System.Delegate var method; var choice = "D"; if(choice=="D") { method = D; } else if(choice=="B") { method = B; } else if(choice=="S") { method = S; } else return; DoSomething(method); // call another method using the method as a delegate. // or instead of calling another method, I want to do: for(int i = 0; i < 20; i++){ SomeArray[i] = method(); }
这可能吗?
我读过这篇文章: 将一个方法存储为C#中一个类的成员变量但是我需要存储具有不同返回类型的方法…
好吧,你可以这样做:
Delegate method; ... if (choice == "D") // Consider using a switch... { method = (Func) D; }