Search code examples
c#arraysmethodsinitializationparameter-passing

Initializing a string array as an argument in a method call in C#


If I have a method like this:

public void DoSomething(int Count, string[] Lines)
{
   //Do stuff here...
}

Why can't I call it like this?

DoSomething(10, {"One", "Two", "Three"});

What would be the correct (but hopefully not the long way)?


Solution

  • you can do this :

    DoSomething(10, new[] {"One", "Two", "Three"});
    

    provided all the objects are of the same type you don't need to specify the type in the array definition