10
如何使用至少有一個值的參數params
?具有至少一個值的C#參數
public void Foo(params string[] s) { }
public void main()
{
this.Foo(); // compile error
this.Foo(new string[0]); // compile error
this.Foo({ }); // compile error
this.Foo("foo"); // no error
this.Foo("foo1", "foo2"); // no error
}
我也想過這個解決方法,我認爲它不是那麼幹淨。因爲'first'字符串不會是'string []'_(如果有的話)的迭代中的一部分,這在某些情況下可能不起作用。沒有其他辦法嗎? –
但是你所說的是,你總是*會有一個參數開始。該解決方案儘可能清晰地反映了這一點。 – Nick
@JohnIsaiahCarmona然後將它們連接在一起:'new [] {first} .Concat(s)'。現在通常可以對結果進行「foreach」。 –