Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# is an object[] as an inline parameter possible?

Tags:

c#

If I have a method declared like this:

private void someFunction(object[] param1) 

When I call this function do I have to declare the object array as a variable, or is there a quicker / shortcut way to just pass it inline to the function call.

I'm doing it like this:

Object[] myParam1 = new Object[2]
myParam1[0] = "blah";
myParam1[1] = "blah blah"; 
someFunction(myParam1); 

In my real code, not this example, I'm calling COM from Marshal, and the code is getting messy each time I have to declare the arguments.

like image 416
JL. Avatar asked Jun 08 '11 21:06

JL.


Video Answer


2 Answers

someFunction(new [] { "blah", "blah blah", "more", "etc" });

like image 177
Chris Walsh Avatar answered Oct 10 '22 22:10

Chris Walsh


Well you could use array initializers that were introduced in C# 3.0:

someFunction(new object[] { "blah", "blah blah" }); 

and if you have your method declared like this:

private void someFunction(params object[] param1)

you could even write:

someFunction("blah", "blah blah"); 
like image 22
Darin Dimitrov Avatar answered Oct 10 '22 20:10

Darin Dimitrov