47 Replies
I mean, the answer to "How do I add elements to a list" is "Use
Add
or AddRange
". If you then say "I don't want to do that", the obvious response is "Why don't you want to do that?"i mean i can guess why, i just want to hear it from them
because

I mean, the obvious solution to that is not to have 50+
const string
members called meaningless things like A24
??????????????????????????????
thats the data so youre telling me to rename it all or?
what "data" is that
where did it come from
theyre object ids
bro someone said its easy with a class but i cant find anything about it online
i mean they didnt say it was easy they said it could be doe
done
Do you need all of those as
const string
members? Can't you just put them in an array to start with?If you must, you can use list initializers
Although I think having all those constants is not very eligant
can i put them in an array without writing all their names one by one, then?
I think you're missing the point of what I'm saying
ʳᵉᶠˡᵉᶜᵗᶦᵒⁿ
I'm saying do:
Rather than:
sssh 😉
i think
static string[] AStrings => ...
is better?
there's some weird stuff with thatyou dont have to create a new instance every time
and i'm not
No? That will create a new array every time it's accessed. Use an
ImmutableArray<string>
or IReadOnlyList<string>
if you want to stop someone re-assigning elementsstatic is weird like that
nope
@Zombie help me out
The compiler disagrees with you: https://sharplab.io/#v2:EYLgxg9gTgpgtADwGwBYA0AXEBLANgHwAEAmARgFgAoQgZgAIS6BhOgbyrs4fsNKQdIAGANoBdOgDEIEOgF4AfHQB2MAO5i2dAEQBDLWm3AtdAL4BuKiaA==
Nothing weird -- it behaves exactly as you'd expect
You might be thinking of the
ReadOnlySpan
optimization
public static ReadOnlySpan<byte> Stuff => new byte[] { ... };
That won't allocate
But it's not specific to static
, you can do the same thing in a local etcman
i knew there was something
just not what
huh
or just do
static byte[] Stuff {get;}=new byte[]{...};
That's not as efficient
wdym
The
ReadOnlySpan
trick will store the bytes in the .data section of the .dll/.exe
And you'll get a span directly over the memory
No allocationforbidden knowledge
oh 

GitHub
Refer directly to static data when ReadOnlySpan wraps arrays of byt...
Refer directly to static data when ReadOnlySpan wraps strings or arrays of primitive literals.
No need to allocate anything in this case.
Fixes:#23358
Related:dotnet/corefx#25413
Ask Mode template ...
GitHub
[Proposal]: ReadOnlySpan initialization from static data · Issue #5...
ReadOnlySpan initialization from static data Proposed Prototype: Not Started Implementation: Not Started Specification: Not Started Summary Provide a syntax for initializing a ReadOnlySpan<T...
this is just allocating it once and then you can access it with a getter. ofc not as good as that trick but better than
=>
why specifically bytes lol
why not any unmanaged T
because bytes aren't subject to endianness
for larger-than-byte types, you need to swap the data if the endianness doesn't match the host
zang
https://learn.microsoft.com/en-us/dotnet/api/system.runtime.compilerservices.runtimehelpers.createspan?view=net-7.0 was added in .NET 7 so the compiler can handle it for more than just bytes
RuntimeHelpers.CreateSpan(RuntimeFieldHandle) Method (System.Runtim...
Provides a fast way to access constant data stored in a module as a ReadOnlySpan.
then just do that 

So
ReadOnlySpan<int> Values => new int[] { ... }
is efficient toothis fucking language lmao
c# is a messy bowl of sugar
This is less syntax sugar and more of a compiler optimization
there is more than sugar in that bowl
If you're assigning an array to a
ReadOnlySpan
, you can't modify it, so the compiler is allowed to just point the span at constant data
And constant data can be stored directly in the executable/library, rather than allocated at runtime
So you get it for "free"
static byte[] Values { get; } = new byte[] { ... }
while still only one allocation, actually does have a costwriting my whole application with readonly spans only and stackalloc from now on 

if you have thousands of those, it will have a significant cost to initialize them all, this was a real problem in TerraFX.Interop.Windows for COM GUIDs
they were all eventually changed to use the ROS<T> trick and it eliminated the startup delay involved with allocating the arrays
cool
sometimes i just do my own com interop
a little less efficient but nicer to use than that