[英]Passing a generic list to a multi-parameter method in C#
Working on a program where I need to pass three pieces of information to a method that will, in turn, create a row for a table from the data passed in. 在需要将三条信息传递给一个方法的程序上,该方法将依次根据传入的数据为表创建一行。
The variables are: 变量是:
var runningTally = new Dictionary<string, Dictionary<string, int>>();
var comments = query.ToList();
var columns = query.ToList();
I'd call the method like this: 我会这样调用该方法:
tReport.Rows.Add(row);
foreach (var key in _masterList.Keys)
{
CreateRow(key, runningTally, columns);
}
I was hoping my method signature could look like this: 我希望我的方法签名看起来像这样:
private void CreateRow(string key, IDictionary<string, Dictionary<string, int>>
runningTally, List<T> columns)
But that doesn't work (the editor complains that the type 'T' can't be found). 但这是行不通的(编辑器抱怨找不到“ T”类型)。 I was hoping someone could tell me how to properly use a generic list as a parameter for a method. 我希望有人能告诉我如何正确地将通用列表用作方法的参数。 I appreciate any help and advice! 我感谢任何帮助和建议!
You need to mark the method as generic. 您需要将方法标记为通用。
Like this: 像这样:
private void CreateRow<T>(string key, IDictionary<string, Dictionary<string, int>>
runningTally, List<T> columns)
Notice the method name itself has the generic parameter added to it. 请注意,方法名称本身已添加了通用参数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.