Can you use the params keyword in a delegate?

后端 未结 2 655
借酒劲吻你
借酒劲吻你 2021-01-03 18:37

I\'d like to define a delegate that takes a couple of dates, an unknown number of other parameters (using the params keyword), and that returns a list of object

2条回答
  •  北荒
    北荒 (楼主)
    2021-01-03 19:20

    You can't use params for any parameter other than the last one... that's part of what it's complaining about.

    You also can't use params in a type argument. This isn't just for delegates, but in general. For example, you can't write:

    List list = new List();
    

    You can, however, declare a new delegate type, like this:

    delegate void Foo(int x, params string[] y);
    
    ...
    
    Foo foo = SomeMethod;
    foo(10, "Hi", "There");
    

    Note that the method group conversion will have to match a method which takes a string array - you couldn't declare SomeMethod as:

    void SomeMethod(int x, string a, string b)
    

    and expect the above to work, for example. It would have to be:

    void SomeMethod(int x, string[] args)
    

    (Or it could use params itself, of course.)

提交回复
热议问题