2017-03-09 160 views
2

如何實現具有可變數量參數的方法?如何實現具有可變數量參數的方法?

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

[在F#中的可能的複製,你如何咖喱ParamArray函數(li ke sprintf)?](http://stackoverflow.com/questions/11145680/in-f-how-do-you-curry-paramarray-functions-like-sprintf) –

回答

8

您可以使用ParamArrayAttribute

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

則:

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

如何一次傳遞數組? – Mohsen

相關問題