C# 每个参数化委托的列表
我试图为列表中的每个成员调用一个函数,但将其他参数传递给委托 如果我有一个名为C# 每个参数化委托的列表,c#,generics,C#,Generics,我试图为列表中的每个成员调用一个函数,但将其他参数传递给委托 如果我有一个名为文档的列表 List<string> documents = GetAllDocuments(); 这需要CallAnotherFunction具有如下定义 public void CallAnotherFunction(string value) { //do something } 但是,我需要调用CallAnotherFunction中的另一个参数,比如content,它依赖于调用列表 所
文档的列表
List<string> documents = GetAllDocuments();
这需要CallAnotherFunction
具有如下定义
public void CallAnotherFunction(string value)
{
//do something
}
但是,我需要调用CallAnotherFunction
中的另一个参数,比如content
,它依赖于调用列表
所以,我的理想定义是
public void CallAnotherFunction(string value, string content)
{
//do something
}
我想把内容作为ForEach调用的一部分传递
List<string> documents = GetAllDocuments();
documents.ForEach(CallAnotherFunction <<pass content>>);
List<string> templates = GetAllTemplates();
templates.ForEach(CallAnotherFunction <<pass another content>>);
List documents=GetAllDocuments();
documents.ForEach(CallAnotherFunction);
列表模板=GetAllTemplates();
templates.ForEach(CallAnotherFunction);
有没有一种方法可以在不必定义不同函数或使用迭代器的情况下实现这一点 使用lambda表达式:
string content = "Other parameter value";
documents.ForEach(x => CallAnotherFunction(x, content));
使用lambda表达式而不是方法组:
List<string> documents = GetAllDocuments();
documents.ForEach( d => CallAnotherFunction(d, "some content") );
List<string> templates = GetAllTemplates();
templates.ForEach( t => CallAnotherFunction(t, "other content") );
List documents=GetAllDocuments();
ForEach(d=>CallAnotherFunction(d,“某些内容”);
列表模板=GetAllTemplates();
ForEach(t=>CallAnotherFunction(t,“其他内容”);
伙计-我觉得自己很傻,这太明显了。干杯
List<string> documents = GetAllDocuments();
documents.ForEach( d => CallAnotherFunction(d, "some content") );
List<string> templates = GetAllTemplates();
templates.ForEach( t => CallAnotherFunction(t, "other content") );