Convert list to params C#

AgresivD picture AgresivD · Mar 21, 2017 · Viewed 13.8k times · Source

I have this following list:

var myList = new List<KeyValuePair<string, object>>();

And this function:

public void Test(params KeyValuePair<string, object>[] list)

How can I do a conversion of the list to params when using the function? Like that:

Test(myList);

Answer

Nikhil Agrawal picture Nikhil Agrawal · Mar 21, 2017

You method declaration KeyValuePair<string, object>[] list states that it will accept an array so you need to convert your list to array like this

Test(myList.ToArray());