collection expression on Span<T>
Just a quick question, wasn't something like
Did they skip anti allocation improvements like this initially? right now it just allocates an array.
5 Replies
It should use
stackalloc
.
Not sure why it's using an array, ask in #roslyn perhaps.@Tacti Tacoz it does not create an array, it creates an inline array https://sharplab.io/#v2:EYLgtghglgdgPgAQEwEYCwAoBAGABAlAOgCEBXAM3IFMAnAZwG5MEBmfJXAYVwG9NcB+NggAsuALIAKAMoAHCDAA8sAC4A+XHXkwAlL36CAvgYEmh+MeKSS9fDIIcTJAbRQBdHU3tHMhoA==
SharpLab
C#/VB/F# compiler playground.
see https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-12#inline-arrays and https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/struct#inline-arrays for details
Structure types - C# reference - C#
Learn about the struct type in C#
What's new in C# 12 - C# Guide - C#
Get an overview of the new features in C# 12.
Hmm maybe it doesn't do it when targeting .net framework?
or net standard for that matter:
.
Not in .net 7.0 either:
It's .net 8.0 only:
I guess because inline array require runtime support.
But was it really too much to ask for them to add a stackalloc fallback though? Seems kinda sloppy to be honest