<p>I have a method which takes params object[] such as:</p>

<pre><code>void Foo(params object[] items)
{
    Console.WriteLine(items[0]);
}
</code></pre>

<p>When I pass two object arrays to this method, it works fine:</p>

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

<p>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:</p>

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

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

[{'answer_id': 36360, 'author': 'Lasse V. Karlsen', 'author_id': 267, 'author_profile': 'https://Stackoverflow.com/users/267', 'pm_score': 2, 'selected': False, 'text': '<p>You need to encapsulate it into another object[] array, like this:</p>\n\n<pre><code>Foo(new Object[] { new object[]{ (object)"1", (object)"2" }});\n</code></pre>\n'}, {'answer_id': 36361, 'author': 'Mike Stone', 'author_id': 122, 'author_profile': 'https://Stackoverflow.com/users/122', 'pm_score': 1, 'selected': False, 'text': '<p>One option is you can wrap it into another array:</p>\n\n<pre><code>Foo(new object[]{ new object[]{ (object)"1", (object)"2" } });\n</code></pre>\n\n<p>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:</p>\n\n<pre><code>Foo((object) new object[]{ (object)"1", (object)"2" });\n</code></pre>\n\n<p>Alternatively, you could try defining another overloaded instance of Foo which takes just a single array:</p>\n\n<pre><code>void Foo(object[] item)\n{\n    // Somehow don\'t duplicate Foo(object[]) and\n    // Foo(params object[]) without making an infinite\n    // recursive call... maybe something like\n    // FooImpl(params object[] items) and then this\n    // could invoke it via:\n    // FooImpl(new object[] { item });\n}\n</code></pre>\n'}, {'answer_id': 36367, 'author': 'Adam Wright', 'author_id': 1200, 'author_profile': 'https://Stackoverflow.com/users/1200', 'pm_score': 8, 'selected': True, 'text': '<p>A simple typecast will ensure the compiler knows what you mean in this case.</p>\n\n<pre><code>Foo((object)new object[]{ (object)"1", (object)"2" }));\n</code></pre>\n\n<p>As an array is a subtype of object, this all works out. Bit of an odd solution though, I\'ll agree.</p>\n'}, {'answer_id': 38123, 'author': 'Emperor XLII', 'author_id': 2495, 'author_profile': 'https://Stackoverflow.com/users/2495', 'pm_score': 6, 'selected': False, 'text': '<p>The <code>params</code> parameter modifier gives callers a shortcut syntax for passing multiple arguments to a method. There are two ways to call a method with a <code>params</code> parameter:</p>\n<p><strong>1)</strong> Calling with an array of the parameter type, in which case the <code>params</code> keyword has no effect and the array is passed directly to the method:</p>\n<pre><code>object[] array = new[] { &quot;1&quot;, &quot;2&quot; };\n\n// Foo receives the \'array\' argument directly.\nFoo( array );\n</code></pre>\n<p><strong>2)</strong> 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:</p>\n<pre><code>// Foo receives a temporary array containing the list of arguments.\nFoo( &quot;1&quot;, &quot;2&quot; );\n\n// This is equivalent to:\nobject[] temp = new[] { &quot;1&quot;, &quot;2&quot; );\nFoo( temp );\n</code></pre>\n<br/>\n<p>In order to pass in an object array to a method with a &quot;<code>params object[]</code>&quot; parameter, you can either:</p>\n<p><strong>1)</strong> Create a wrapper array manually and pass that directly to the method, as mentioned by <a href="https://stackoverflow.com/questions/36350/c-how-to-pass-a-single-object-to-a-params-object#36360">lassevk</a>:</p>\n<pre><code>Foo( new object[] { array } );  // Equivalent to calling convention 1.\n</code></pre>\n<p><strong>2)</strong> Or, cast the argument to <code>object</code>, as mentioned by <a href="https://stackoverflow.com/questions/36350/c-how-to-pass-a-single-object-to-a-params-object#36367">Adam</a>, in which case the compiler will create the wrapper array for you:</p>\n<pre><code>Foo( (object)array );  // Equivalent to calling convention 2.\n</code></pre>\n<br/>\n<p>However, if the goal of the method is to process multiple object arrays, it may be easier to declare it with an explicit &quot;<code>params object[][]</code>&quot; parameter. This would allow you to pass multiple arrays as arguments:</p>\n<pre><code>void Foo( params object[][] arrays ) {\n  foreach( object[] array in arrays ) {\n    // process array\n  }\n}\n\n...\nFoo( new[] { &quot;1&quot;, &quot;2&quot; }, new[] { &quot;3&quot;, &quot;4&quot; } );\n\n// Equivalent to:\nobject[][] arrays = new[] {\n  new[] { &quot;1&quot;, &quot;2&quot; },\n  new[] { &quot;3&quot;, &quot;4&quot; }\n};\nFoo( arrays );\n</code></pre>\n<hr />\n<p><strong>Edit:</strong> Raymond Chen describes this behavior and how it relates to the C# specification in <a href="https://devblogs.microsoft.com/oldnewthing/20130806-00/?p=3603" rel="nofollow noreferrer">a new post</a>.</p>\n'}, {'answer_id': 11148337, 'author': 'HoBa', 'author_id': 662165, 'author_profile': 'https://Stackoverflow.com/users/662165', 'pm_score': 1, 'selected': False, 'text': '<pre><code>new[] { (object) 0, (object) null, (object) false }\n</code></pre>\n'}, {'answer_id': 35474769, 'author': 'ACOMIT001', 'author_id': 3713362, 'author_profile': 'https://Stackoverflow.com/users/3713362', 'pm_score': 3, 'selected': False, 'text': '<p>This is a one line solution involving LINQ.</p>\n\n<pre><code>var elements = new String[] { "1", "2", "3" };\nFoo(elements.Cast&lt;object&gt;().ToArray())\n</code></pre>\n'}, {'answer_id': 49778932, 'author': 'Zhuravlev A.', 'author_id': 9112327, 'author_profile': 'https://Stackoverflow.com/users/9112327', 'pm_score': 2, 'selected': False, 'text': "<p>Another way to solve this problem (it's not so good practice but looks beauty):</p>\n\n<pre><code>static class Helper\n{\n    public static object AsSingleParam(this object[] arg)\n    {\n       return (object)arg;\n    }\n}\n</code></pre>\n\n<p>Usage:</p>\n\n<pre><code>f(new object[] { 1, 2, 3 }.AsSingleParam());\n</code></pre>\n"}]
['https://Stackoverflow.com/questions/36350', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/31505/']