0

I recall that in C# a notation similar to the one below would be a correct array initializer.

int[] array = new int[3]
{
  [1] = 5;
};

This should initialize the array with all values as default (0 in the case of int) and assign 5 to index 1.

The equivalent would be

int[] array = {0, 5, 0};

This would be correct with a type other than int[] that implements an indexer, but how would I do it with an array?

5
  • 1
    You mean C with first snippet, don't you? Commented May 15, 2020 at 10:14
  • 2
    Does this answer your question? All possible array initialization syntaxes Commented May 15, 2020 at 10:15
  • That is exactly what I remember. So.. it works in C, but not C# I guess Commented May 15, 2020 at 10:16
  • @prenone exactly in C# there isn't that type of initialization, in this case you can solve in the way you have already done or set the value right after the initialization. Commented May 15, 2020 at 10:25
  • Okay, that is what I was afraid of. Thank you very much for clarifying Commented May 15, 2020 at 11:02

0

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.