2

I have the following C# method:

private string[] test()
{
}

I would like to know if I can do something like this within the method:

return {"1","2"}

instead of:

string[] quantity = { "1","2" };
return quantity;

Kindly let me know how I can do this in C#.

2
  • 1
    Even if it could be done (no, it can't) I'm fairly sure there would be no difference in memory usage. What are you trying to accomplish ? Commented Mar 22, 2012 at 8:13
  • While a serious question the compiler should the same optimised code for both situation. So you're just saving a couple a characters in a test method. Not something I would do for production code (hard coded array's anyway). And I can see a situation in the future you'll need that local variable anyway. Commented Mar 22, 2012 at 8:58

3 Answers 3

6
return new string[] { "1", "2" };
Sign up to request clarification or add additional context in comments.

Comments

4

The shortest way:

return new[] { "1", "2" };

Comments

1

You mean:

private string[] test()
{
    return new string[] { "1", "2", "3" };
}

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.