在下面的代码中,我将B方法作为一个操作传递给IterateObjects方法中的对象。我想问一下是否可以在参数中显式声明方法,而不是通过名称传递它,就像这样:
a.IterateObjects(delegate void(string s){//method body})
它是不正确的,但我确信我见过这样的东西可以工作。请问您能否提供建议?谢谢。DelTest a = new DelTest(); //class with method IterateObjects
a.IterateObjects(B) //HERE
private void B(string a)
{
listBox1.Items.Add(a);
}
//another class ....
public void IterateObjects(Action<string> akce)
{
foreach(string a in list)
{
akce(a);
}
}