使用object []和object [] []作为参数的C#调用方法(params object [] [])(C# call method (params object[][]) with object[] and object[][] as parameters)
我有一个方法
public void method(params object[][] arg)但是在我调用方法的地方,我有对象[]和对象[] []:
object[] var1 = new object[5]; object[][] var2 = new object[8][5]; method(var1, var2); (I´d like this to be method(var1, var2[0], var2[1], var2[2],...)但是在内部方法中,arg的长度为2:
arg[0] = var1 arg[1] = var2但我希望arg作为长度为9的数组:
arg[0] = var1 arg[1] = var2[0]; arg[2] = var2[1]; ...我怎么称呼这个方法?
I have a method
public void method(params object[][] arg)But where I call the method, I have and object[] and object[][]:
object[] var1 = new object[5]; object[][] var2 = new object[8][5]; method(var1, var2); (I´d like this to be method(var1, var2[0], var2[1], var2[2],...)But inside method, arg has length 2:
arg[0] = var1 arg[1] = var2But I want arg as an array of length 9:
arg[0] = var1 arg[1] = var2[0]; arg[2] = var2[1]; ...How can I call the method?
最满意答案
这听起来像你想要的:
object[][] args = new[] { var1 }.Concat(var2).ToArray(); method(args);C#的“普通”参数数组处理不会进行任何展平:您可以使用单个参数调用它,该参数已经是正确的类型, 或者使用元素类型的参数序列调用它。 你不能混搭。
It sounds like you want:
object[][] args = new[] { var1 }.Concat(var2).ToArray(); method(args);The "normal" parameter array handling of C# doesn't do any flattening: you either call it with a single argument which is already the right type, or you call it with a sequence of arguments of the element type. You can't mix and match.
更多推荐
发布评论