Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python-like list unpacking in C#?

In python, I can do something like this:

List=[3, 4]

def Add(x, y):
    return x + y

Add(*List) #7

Is there any way to do this or something similar in C#? Basically I want to be able to pass a List of arguments to an arbitrary function, and have them applied as the function's parameters without manually unpacking the List and calling the function explicitly specifying the parameters.

like image 538
ryeguy Avatar asked Feb 20 '09 04:02

ryeguy


1 Answers

Well, the closest would be reflection, but that is on the slow side... but look at MethodInfo.Invoke...

like image 66
Marc Gravell Avatar answered Oct 15 '22 08:10

Marc Gravell