Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# - How do I use an array of arrays as input for "parameterized" method?

I want to create a method that accepts a "parameterized" input object of type string array of string array. Something like:

public void MyMethod(params string[][] input)
{
   //...do stuff
}

I am calling this method as follows:

MyMethod({"arry1-elem1","arry1-elem2"}, {"arry2-elem1","arry2-elem2"}, {"arry3-elem1","arry3-elem2"});

However, when I do this I get the following error:

Invalid expression term '{'

What am I doing wrong here. Is it not possible to input an implicitly typed array as an input?

like image 870
Hooplator15 Avatar asked Mar 13 '23 01:03

Hooplator15


2 Answers

MyMethod(new string[]{"arry1-elem1","arry1-elem2"}, new string[]{"arry2-elem1","arry2-elem2"}, new string[]{"arry3-elem1","arry3-elem2"});

You aren't declaring their type when you're attempting to pass them in.

like image 127
Dispersia Avatar answered Mar 18 '23 07:03

Dispersia


Even better, you don't have to say string:

MyMethod(new[]{"a","b"}, new[]{"c","d"});
like image 29
DLeh Avatar answered Mar 18 '23 07:03

DLeh