[英]C# passing an Action<> into another Action<>
我是代表的新手,我正在嘗試將Action <>作為另一個Action <>的參數傳遞。 例如,(考慮到代碼的可重用性)我嘗試進行循環操作並將其他操作傳遞到其中,以減少代碼中的循環次數:
Action<int, Action<Control, Control>> loop = (int stop, Action<Control, Control> action) =>
{
for (int i = 0; i < stop; i++)
{
for (int j = 0; j < stop; j++)
{
<action> pass Add;
}
}
};
Action<Control, Control> Add = (Control Parent, Control Child) => Parent.Controls.Add(Child);
主要目標是能夠以這種方式重復使用double for循環,以便我可以將其內部的動作與其他動作進行混合和匹配。
Action<int, Action<Control, Control>> loop = (int stop, Action<Control, Control> action) =>
{
for (int i = 0; i < stop; i++)
{
for (int j = 0; j < stop; j++)
{
//// invoke action with real arguments
// action(controlParent, controlChild);
}
}
};
Action<Control, Control> Add = (Control Parent, Control Child) => Parent.Controls.Add(Child);
loop(1, Add);
如果要在主動作中使用另一個動作來循環,您的動作用法也不錯。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.