Spread operator when using Add innitalization syntax #9973
Unanswered
Dovencio
asked this question in
Language Ideas
Replies: 3 comments 4 replies
-
|
The spread operator is specific to collection expressions and has not been implemented anywhere else in the language. |
Beta Was this translation helpful? Give feedback.
0 replies
-
Beta Was this translation helpful? Give feedback.
1 reply
-
Do this instead: var foo = new Foo
{
FooStrings = [.. str1, "d", "w", .. str2]
};The point of collection expressions is to supersede and unify all the varying ways the language does collection initialization. It's def not 100% complete. But cases like this are better served with the above approach. |
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Say I have a class:
And some arrays of strings:
Say I want to add
str1andstr2toFoo.FooStringsusing the initialization syntax.But right now that gives the error
Cannot implicitly convert type 'string[]' to 'System.Index'.I expected it to be the equivalent of:
Is there a reason why the spread (
..) syntax is not supported for the add initialization ({ ... }) syntax or am I missing something?Beta Was this translation helpful? Give feedback.
All reactions