Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing array of Argument into multiple parameter function in C#

Life is short

def foo(b,a,r):
    # bla bla bla
    pass

mylist = ['b','a','r']
foo(*mylist) 

That's how passing a list of arguments into method multiple positional parameters in Python.

But in C# I found out passing an array into a parameter of array using the params keywords, like this one:

void foo(params string[] bar)
{
    // bla bla bla
}

string[] mylist = {"b", "a", "r"};
foo(mylist)

Imagine how If I cannot touch the function, is there an easier way Is there a way to do that?

Here is what I mean:

void foo (string b, string a, string r)
{
    // bla bla bla
}

string[] mylist = {"b", "a", "r"};
foo(mylist[0],mylist[1],mylist[2]); // This line makes my life difficult

I search online and can't found an alternative of it. Just want to make sure that I didn't miss the hidden feature that C# provide. Who know there's such shortcut for us?

like image 409
Yeo Avatar asked Feb 11 '12 17:02

Yeo


People also ask

Can you pass arrays as function arguments in C?

In C programming, you can pass an entire array to functions.

Can one dimensional array be passed as function arguments in C language?

An array can be passed to functions in C using pointers by passing reference to the base address of the array, and similarly, a multidimensional array can also be passed to functions in C.

Can you pass arrays as parameters?

Arrays can be passed as arguments to method parameters. Because arrays are reference types, the method can change the value of the elements.


2 Answers

No, there is no feature in C# that allows you to do this directly. But there are some ways you can use to work around that:

  1. Create a method that converts a method with several parameters into a method with one array parameter:

    static Action<T[]> ToArrayMethod<T>(Action<T, T, T> original)
    {
        return array => original(array[0], array[1], array[2]);
    }
    

    And then use it like this:

    string[] array = {"b", "a", "r"};
    
    var arrayFoo = ToArrayMethod(foo);
    
    arrayFoo(array);
    

    The downside of this approach is that this method works only on methods with exactly three parameters with a void return type. You would need to write another overload of ToArrayMethod() if you wanted this to work for methods with, say, 4 parameters, or for those that return some value.

  2. Use reflection. When doing that, arguments are passed in using an array:

    var foo = typeof(SomeType)
        .GetMethod("foo", BindingFlags.Instance | BindingFlags.NonPublic);
    
    object[] array = {"b", "a", "r"};
    
    foo.Invoke(somTypeInstance, array);
    
like image 118
svick Avatar answered Oct 04 '22 13:10

svick


There is downsides to using static languages, and this is one of them, if your function signature is defined as:

void foo (string a, string b, string c);

then you are forced to pass in 3 separate strings

foo(mylist[0],mylist[1],mylist[2]);

the only way to have flexible inputs in C# is by using params which you already now, or by using default values (C# 4.0):

void foo (string a, string b = null, string c = null)

meaning that if I don't pass b or c just set them to null, and now you can call your function like this:

foo(mylist[0]) 
like image 27
Bassam Mehanni Avatar answered Oct 04 '22 13:10

Bassam Mehanni