C# Method Using Params Keyword
-
22-09-2019 - |
Question
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);
}
}
Solution
public static void MyMethod(params Foo[] values) { }
OTHER TIPS
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) { }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow