如何使用可变数量的参数实现方法?
In C#, we can use the params keyword:
public class MyClass
{
public static void UseParams(params int[] list)
{
for (int i = 0; i < list.Length; i++)
{
Console.Write(list[i] + " ");
}
Console.WriteLine();
}
}
那我怎么能在F#中做到这一点?
type MyClass() =
member this.SomeMethod(params (args:string array)) = ()
我从上面的代码中收到以下错误:
The pattern discriminator 'params' is not defined
答案 0 :(得分:8)
您可以使用ParamArrayAttribute
:
type MyClass() =
member this.SomeMethod([<ParamArray>] (args:string array)) = Array.iter (printfn "%s") args
然后:
let mc = MyClass()
mc.SomeMethod("a", "b", "c")