How to pass an array and a single element to a multiple argument method?

后端 未结 4 1412
你的背包
你的背包 2021-01-18 08:59

Example:

public void foo(params string[] s) { ... }

We can call this method with:

a) foo(\"test\", \"test2\", \"test3\") //         


        
4条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-18 09:24

    Just add your string to the array:

    var newArray = new string[oldArray.length+1];
    newArray[0]=yourString;
    oldArray.CopyTo(newArray, 1);
    
    foo(newArray);
    

提交回复
热议问题