Kevin
Kevin

Reputation: 4848

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!

Upvotes: 1

Views: 1276

Answers (1)

rossipedia
rossipedia

Reputation: 59367

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.

Upvotes: 5

Related Questions