简体   繁体   中英

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). 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.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM