C# Method Using Params Keyword
-
22-09-2019 - |
문제
An example of a method that uses the params keyword is String.Format("", foo, bar, baz)
But how would I make a method that accepts an array of enums like so:
class MyClass
{
public enum Foo { Bar, Baz }
public static void MyMethod(params enum[] Foo) {}
public static void TestMethod()
{
MyMethod();
MyMethod(Foo.Bar);
MyMethod(Foo.Baz);
MyMethod(Foo.Bar, Foo.Baz);
}
}
해결책
public static void MyMethod(params Foo[] values) { }
다른 팁
Try this instead
class MyClass
{
public enum Foo { Bar, Baz }
public static void MyMethod(params Foo[] foos) {}
public static void TestMethod()
{
MyMethod();
MyMethod(Foo.Bar);
MyMethod(Foo.Baz);
MyMethod(Foo.Bar, Foo.Baz);
}
}
Err..try:
public static void MyMethod(params Foo[] foo) { }
제휴하지 않습니다 StackOverflow