如何实现具有可变数量的参数的方法? [英] How do I implement a method with a variable number of arguments?

查看:65
本文介绍了如何实现具有可变数量的参数的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

在C#中,我们可以使用params关键字:

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#中做到这一点呢?

So how can I do this in F#?

type MyClass() =

    member this.SomeMethod(params (args:string array)) = ()

我从上面的代码中收到以下错误:

I receive the following error from the code above:

The pattern discriminator 'params' is not defined

推荐答案

您可以使用

You can use ParamArrayAttribute:

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

然后:

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

这篇关于如何实现具有可变数量的参数的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆