C# 如何将单个对象 [] 传递给参数对象 []

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36350/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 09:43:38  来源:igfitidea点击:

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

提问by Serhat Ozgel

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

我有一个采用 params object[] 的方法,例如:

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

When I pass two object arrays to this method, it works fine:

当我将两个对象数组传递给这个方法时,它工作正常:

Foo(new object[]{ (object)"1", (object)"2" }, new object[]{ (object)"3", (object)"4" } );
// Output: System.Object[]

But when I pass a single object[], it does not take my object[] as the first param, instead it takes its all elements like I wanted to pass them one by one:

但是当我传递单个对象 [] 时,它不会将我的对象 [] 作为第一个参数,而是将其所有元素都传递给我,就像我想一个一个地传递它们一样:

Foo(new object[]{ (object)"1", (object)"2" });
// Output: 1, expected: System.Object[]

How do I pass a single object[] as a first argument to a params array?

如何将单个 object[] 作为第一个参数传递给 params 数组?

采纳答案by Adam Wright

A simple typecast will ensure the compiler knows what you mean in this case.

在这种情况下,简单的类型转换将确保编译器知道您的意思。

Foo((object)new object[]{ (object)"1", (object)"2" }));

As an array is a subtype of object, this all works out. Bit of an odd solution though, I'll agree.

由于数组是对象的子类型,所以这一切都成立。虽然有点奇怪的解决方案,但我同意。

回答by Lasse V. Karlsen

You need to encapsulate it into another object[] array, like this:

您需要将其封装到另一个 object[] 数组中,如下所示:

Foo(new Object[] { new object[]{ (object)"1", (object)"2" }});

回答by Mike Stone

One option is you can wrap it into another array:

一种选择是您可以将其包装到另一个数组中:

Foo(new object[]{ new object[]{ (object)"1", (object)"2" } });

Kind of ugly, but since each item is an array, you can't just cast it to make the problem go away... such as if it were Foo(params object items), then you could just do:

有点难看,但是由于每个项目都是一个数组,因此您不能仅将其强制转换以使问题消失......例如如果它是 Foo(params object items),那么您可以这样做:

Foo((object) new object[]{ (object)"1", (object)"2" });

Alternatively, you could try defining another overloaded instance of Foo which takes just a single array:

或者,您可以尝试定义另一个只需要一个数组的 Foo 重载实例:

void Foo(object[] item)
{
    // Somehow don't duplicate Foo(object[]) and
    // Foo(params object[]) without making an infinite
    // recursive call... maybe something like
    // FooImpl(params object[] items) and then this
    // could invoke it via:
    // FooImpl(new object[] { item });
}

回答by Emperor XLII

The paramsparameter modifier gives callers a shortcut syntax for passing multiple arguments to a method. There are two ways to call a method with a paramsparameter:

params参数修改给呼叫者传递多个参数的方法的快捷语法。有两种方法可以调用带params参数的方法:

1)Calling with an array of the parameter type, in which case the paramskeyword has no effect and the array is passed directly to the method:

1)使用参数类型的数组调用,此时params关键字无效,直接将数组传递给方法:

object[] array = new[] { "1", "2" };

// Foo receives the 'array' argument directly.
Foo( array );

2)Or, calling with an extended list of arguments, in which case the compiler will automatically wrap the list of arguments in a temporary array and pass that to the method:

2)或者,使用扩展的参数列表调用,在这种情况下,编译器会自动将参数列表包装在一个临时数组中并将其传递给方法:

// Foo receives a temporary array containing the list of arguments.
Foo( "1", "2" );

// This is equivalent to:
object[] temp = new[] { "1", "2" );
Foo( temp );



In order to pass in an object array to a method with a "params object[]" parameter, you can either:

为了将对象数组传递给带有“ params object[]”参数的方法,您可以:

1)Create a wrapper array manually and pass that directly to the method, as mentioned by lassevk:

1)手动创建一个包装器数组并将其直接传递给方法,如lassevk 所述

Foo( new object[] { array } );  // Equivalent to calling convention 1.

2)Or, cast the argument to object, as mentioned by Adam, in which case the compiler will create the wrapper array for you:

2)或者,objectAdam所述,将参数强制转换为,在这种情况下,编译器将为您创建包装器数组:

Foo( (object)array );  // Equivalent to calling convention 2.



However, if the goal of the method is to process multiple object arrays, it may be easier to declare it with an explicit "params object[][]" parameter. This would allow you to pass multiple arrays as arguments:

但是,如果该方法的目标是处理多个对象数组,则使用显式“ params object[][]”参数声明它可能更容易。这将允许您将多个数组作为参数传递:

void Foo( params object[][] arrays ) {
  foreach( object[] array in arrays ) {
    // process array
  }
}

...
Foo( new[] { "1", "2" }, new[] { "3", "4" } );

// Equivalent to:
object[][] arrays = new[] {
  new[] { "1", "2" },
  new[] { "3", "4" }
};
Foo( arrays );


Edit:Raymond Chen describes this behavior and how it relates to the C# specification in a new post.

编辑:Raymond Chen 在一篇新文章中描述了这种行为以及它与 C# 规范的关系

回答by Homero Barbosa

new[] { (object) 0, (object) null, (object) false }

回答by ACOMIT001

This is a one line solution involving LINQ.

这是涉及 LINQ 的单行解决方案。

var elements = new String[] { "1", "2", "3" };
Foo(elements.Cast<object>().ToArray())

回答by Zhuravlev A.

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