tags:

views:

93

answers:

4

In ruby you can do something like this:

def method(a, b) ... end
myMethod(*myArray)

So if myArray had 2 items, it would be the equivalent of this:

myMehtod(myArray[0], myArray[1])

So that in the method body, a == myArray[0] and b == myArray[1]

Can you do this in C#? (So I can have a method declared with explicit arguments, instead of just taking an array as the argument)

EDIT: I should've been more specific about the method being called.

+5  A: 

If you declare your function's arguments with the params keyword, you can pass in an array or an arbitrary number of individual parameters.

For instance:

public class Foo
{
    public static void Main()
    {
        int[] x = new int[] {1,2,3};
        Bar(x);
        Bar(10,11,12);
    }

    public static void Bar(params int[] quux)
    {
        foreach(int i in quux)
        {
            System.Console.WriteLine(i);
        }
    }
}

produces

1
2
3
10
11
12
Mark Rushakoff
+6  A: 

Your method can be declared to accept a parameter array, via params:

void F(params int[] foo) {
    // Do something with foo.
}

Now you can either pass a arbitrary number of ints to the method, or an array of int. But given a fixed method declaration, you cannot expand an array on-the-fly as you can in Ruby, because the parameters are handled differently under the hood (I believe this is different in Ruby), and because C# isn’t quite as dynamic.

Theoretically, you can use reflection to call the method to achieve the same effect, though (reflected method calls always accept a parameter array).

Konrad Rudolph
I might have to end up using the reflected thing to make it work the way I want, but this is interesting. Thanks!
Daniel Huckstep
A: 

You could do this:

void MyMethod(params MyType[] args)
{
}


MyMethod(myObject1, myObject2, myObject3);

MyType[] a = { new MyType(),  new MyType() };
MyMethod(a);
EricSchaefer
+1  A: 

No, you can't have the array "auto-expand" when passed as an argument to a C# method. One way of simulating this is writing method overloads:

MyMethod(int a, int b) { /* ... */ }

MyMethod(int[] c)
{
   // check array length?
   MyMethod(c[0], c[1]);
}

AnotherMethod()
{
   int[] someArray = new[] {1,2};

   MyMethod(someArray); // valid
   MyMethod(1,2);       // valid
}

But as a few others have already mentioned, it is simpler (and somewhat the reverse) to use the params keyword. In my example (and yours), you always end up having separate a and b. With params you always have an array to deal with.

Lucas