I am new to delegates and I am trying to pass an Action<> as a parameter of another Action<>. For example, (with code re-usability in mind) I was attempting to make a loop action and pass other actions into it to reduce the number of loops in my code:
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);
The main objective is to be able to re-use the double for loop in such a way where I can mix and match the action inside of it with other actions.
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);
如果要在主动作中使用另一个动作来循环,您的动作用法也不错。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.