[Proposal] Designated initializers for arrays #8818
Replies: 12 comments 1 reply
-
This is already legal for any other type with an indexer (e.g. dictionary). I think this makes perfect sense purely for consistency. |
Beta Was this translation helpful? Give feedback.
-
Exactly. I am also confused because according to Arrays as Objects (C# Programming Guide)
and thus should have an indexer implemented (in the abstract Array class) that is accessible during initializations.
Can anyone confirm? |
Beta Was this translation helpful? Give feedback.
-
Array initializers are necessarily different from collection initializers. Collection and indexer initializers came much later in the language and both assume that the collection is responsible for allocation space to add the new elements as they are added. Arrays need to know their size up-front, and array initializers can infer the size based on the elements, so it makes sense that they play by different rules. I could see indexer initializers working with arrays, but I think that the language would have to require that you specify a size. Question is whether or not there's enough value in actually doing that. It's not enough to make it seem more consistent, especially when it can't be consistent. |
Beta Was this translation helpful? Give feedback.
-
Yes, array size should be specified before using a designated initializer. |
Beta Was this translation helpful? Give feedback.
-
Also, again personally, I think consistency should be important between arrays and collections, as they are essentially both groups of elements with very similar syntax. |
Beta Was this translation helpful? Give feedback.
-
C# is loaded with inconsistencies, many of them necessary. Even here an indexer initializer for an array would have to be a special case because arrays aren't like other collections and array initializers aren't like collection initializers. |
Beta Was this translation helpful? Give feedback.
-
I think many implemented things are special cases, it is a matter of wheter it would make sense to allow this specific special case. |
Beta Was this translation helpful? Give feedback.
-
Cost :) Modifying the language is very expensive. If a feature doesn't have sufficient upsides it generally doesn't get considered. I do agree, other than dealing with requiring a size for the array (which is already an inconsistency with array initializers), this doesn't seem terribly controversial, but it's up to someone on the language team to deem it worth of triaging it and considering it for the language. |
Beta Was this translation helpful? Give feedback.
-
Oh yes, sorry, that is most definitely true😅. I agree that it is up the team of course, also I wouldn't consider the proposed feature nor critical nor game-breaking, just something convenient. |
Beta Was this translation helpful? Give feedback.
-
Note: this feature is somehow useful with enums: static readonly int[] fastEnumMap = new int[]
{
[(int)MyEnum.A] = 1,
[(int)MyEnum.B] = 2,
}; |
Beta Was this translation helpful? Give feedback.
-
I think this would be redundant as you can give a intger value to your enum component when declaring them. However it could be interesting to see it done with a different type. E.g
EDIT: I think that Dictionary better suits this need as it also preserve the enum type |
Beta Was this translation helpful? Give feedback.
-
I just came to open this exact same discussion with the exact same proposal. 5 years later, any interest? |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Designated initializers
A great description of designated initializers is given in the first lines of https://gcc.gnu.org/onlinedocs/gcc/Designated-Inits.html
Implementation
The use of designated initializers should be implemented to be used like this:
which will give an array equivalent to
int[] array = new int[5] {1, 0, 0, 1, 0 };
Values of indexes that are not specified in the designated implementation (1, 2, 4 in the example) should be initialized to the default inner type value.
This should also work with multidimensional arrays:
which will give an array equivalent to
int[,] matrix = new int[2,2] { {0, 1}, {0, 1}}
.and jagged arrays:
Alternatives
The obvious alternative to designated initializations is assigning values after the array has been initialized
I apologize for any miscomprehension or error I generate, I am just now gaining experience with GitHub
Beta Was this translation helpful? Give feedback.
All reactions