Proposal: syntactic sugar for initializing Span<char> by stackalloc with string literal #8761
Replies: 5 comments
-
Why? Are there any cases where memory consumed by string literals is an issue? |
Beta Was this translation helpful? Give feedback.
-
@svick I read this as wanting to initialize the space from a string literal but then using it as scratch space. |
Beta Was this translation helpful? Give feedback.
-
Oh, so the equivalent of the following code? string s = "initial characters";
Span<char> span = stackalloc char[s.Length];
s.AsSpan().CopyTo(span); Is that actually so common that it needs syntactic sugar? |
Beta Was this translation helpful? Give feedback.
-
I do not think so. I did not even know about Span in c#. |
Beta Was this translation helpful? Give feedback.
-
@svick That's what I understood @acid-chicken to mean. I haven't run into this pattern before, though I have been in situations where I'd copy a literal to the beginning of a longer buffer. If the stack space isn't going to be mutated after copying the literal into it, I'd just get the pointer to the literal directly without allocating stack space and doing the copy (or use ReadOnlySpan). |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Summary
Provide a syntactic sugar which initializes Span and ReadOnlySpan with stackalloc keyword by string literal.
Design
will be converted to
Choices
Syntax Style
or
or
or other?
Also Support
char[]
Also adding syntactic suger for
char[]
initializer?Beta Was this translation helpful? Give feedback.
All reactions