Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 每个参数化委托的列表_C#_Generics - Fatal编程技术网

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") );