如何实现具有可变数量参数的方法?

How do I implement a method with a variable number of arguments?

如何实现参数数量可变的方法?

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

您可以使用 ParamArrayAttribute:

type MyClass() =
    member this.SomeMethod([<ParamArray>] (args:string array)) = Array.iter (printfn "%s") args

然后:

let mc = MyClass()
mc.SomeMethod("a", "b", "c")