weigsi
weigsi

Reputation: 11

c# call a method with paramaters as a string variable

Hi i want to call a method:

public string MyMethod($MyVariable)
{
}

Where the variable $MyVariable contains all arguments for the function i.e.:

$MyVariable = "argument1,argument2,argumentn"

Is this possible, do i Need Special Syntax?

Upvotes: 0

Views: 834

Answers (1)

Equalsk
Equalsk

Reputation: 8194

I can't tell if you want to pass one string containing all parameters, or multiple parameters.

Single parameter

public void Main()
{
    MyMethod("argument1, argument2, ...");
}

public string MyMethod(string parameters)
{
    Console.Write(parameters);

    return "whatever your string was";
}

Output:

argument1, argument2, ...

Multiple parameters

public void Main()
{
    MyMethod("argument1", "argument2", "...");
}

public string MyMethod(params string[] parameters)
{
    foreach (var parameter in parameters)
    {
        Console.Write(parameter);
    }

    return "whatever your string was";
}

Output:

argument1
argument2
...

Upvotes: 2

Related Questions