Search code examples
c#listparameterskeyvaluepair

Convert list to params C#


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

Solution

  • 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());