0

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?

3
  • nope... just put string before the Myvariable... and lose the $. Commented Mar 30, 2017 at 9:32
  • 2
    public string MyMethod(params string[] parameters) Commented Mar 30, 2017 at 9:33
  • 1
    Show us some code that compiles, the $ thing makes no sense. Are you sure this is C#? Commented Mar 30, 2017 at 9:33

1 Answer 1

2

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
...

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.