How to pass a single object[] to a params object[]

前端 未结 7 1502
有刺的猬
有刺的猬 2020-11-27 15:43

I have a method which takes params object[] such as:

void Foo(params object[] items)
{
    Console.WriteLine(items[0]);
}

When I pass two o

7条回答
  •  盖世英雄少女心
    2020-11-27 16:09

    Another way to solve this problem (it's not so good practice but looks beauty):

    static class Helper
    {
        public static object AsSingleParam(this object[] arg)
        {
           return (object)arg;
        }
    }
    

    Usage:

    f(new object[] { 1, 2, 3 }.AsSingleParam());
    

提交回复
热议问题