C # Method using Params Keyword

An example of a method using the params keyword is String.Format("", foo, bar, baz)

But how can I make a method that accepts an array of enums, for example:

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);
    }
}
+3
source share
3 answers
public static void MyMethod(params Foo[] values) { }
+10
source

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);
}

}

+3
source

Err..try:

public static void MyMethod(params Foo[] foo) { }
+3
source

Source: https://habr.com/ru/post/1734294/


All Articles