Javascript/Typescript style spread operator #4779
Answered
by
333fred
JessieWadman
asked this question in
Language Ideas
-
Whenever I switch between TypeScript and C#, I find myself missing the spread operator in C#. // Arrays
var array1 = new int[] { 1, 2, 3 };
var array2 = new int[] { 4, 5, 6 };
var array3 = new int[] { 7, 8, 9 };
var union = new int[] { ...array1, ...array2, ...array3[..^2], 10, 11 }; // Anonymous objects
// Used to generate a new, anonymous type by combining objects from various sources into a result in an ASP.NET Core controller action
public async Task<ActionResult> Get()
{
var person = myDb.GetPersonById(1); // { id: 1, name: "John Doe" }
var addresses = myDb.GetAddressesById(1); // { addresses: [ { street: "1 Microsoft Way" } ] }
return Ok(new { ...person, ...addresses }); // { id: 1, name: "John Doe", addresses: [ { street: "1 Microsoft Way" } ] }
} |
Beta Was this translation helpful? Give feedback.
Answered by
333fred
May 26, 2021
Replies: 1 comment
-
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
333fred
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Duplicate of #2879 and #3648.