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?

prenone
  • 302
  • 2
  • 20
  • 1
    You mean [C](https://stackoverflow.com/a/38860092/1997232) with first snippet, don't you? – Sinatr May 15 '20 at 10:14
  • 2
    Does this answer your question? [All possible array initialization syntaxes](https://stackoverflow.com/questions/5678216/all-possible-array-initialization-syntaxes) – Sinatr May 15 '20 at 10:15
  • That is exactly what I remember. So.. it works in C, but not C# I guess – prenone May 15 '20 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. – Stefano Cavion May 15 '20 at 10:25
  • Okay, that is what I was afraid of. Thank you very much for clarifying – prenone May 15 '20 at 11:02

0 Answers0