![](/img/trans.png)
[英]How to access the value of a parameter from an Action<> called with a lambda expression in C# and store it in an array
[英]Is it possible to store lambda expression in array C#
我正在编写一个游戏AI引擎,我想在一个数组中存储一些lambda表达式/委托(多个参数列表)。
像这样的东西:
_events.Add( (delegate() { Debug.Log("OHAI!"); }) );
_events.Add( (delegate() { DoSomethingFancy(this, 2, "dsad"); }) );
在C#中有可能吗?
您可以改为List<Action>
:
List<Action> _events = new List<Action>();
_events.Add( () => Debug.Log("OHAI!")); //for only a single statement
_events.Add( () =>
{
DoSomethingFancy(this, 2, "dsad");
//other statements
});
然后调用单个项目:
_events[0]();
你可以使用System.Action。
var myactions = new List<Action>();
myactions .Add(new Action(() => { Console.WriteLine("Action 1"); })
myactions .Add(new Action(() => { Console.WriteLine("Action 2"); })
foreach (var action in myactions)
action();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.