使用object []和object [] []作为参数的C#调用方法(params object [] [])(C# call method (params object[][]) with obj

编程入门 行业动态 更新时间:2024-10-24 08:24:51
使用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] = var2

But 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.

更多推荐

本文发布于:2023-08-04 04:21:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1410178.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:参数   方法   object   params   parameters

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!