Variable size value types #94833
-
Currently, at least from what I know, value types a fixed in size. Especially But very often you have an array of vectors, you need to be able to marshal somehow to structs. So for example we have images where each pixel can contain one component (grayscale), 3 components (RGB), 4 components (RGBA). Currently each needs to be treated differently. Additionally, the datatype can change, so simply writing a switch-case around it using either a There are other requests that have similar requirements dotnet/csharplang#7508. But obviously the runtime needs to do some work. My proposal would be to allow something like public readonly ref struct FixedStruct<T, size> where T : struct where size : const int
{
public readonly Span<T> Value = stackalloc T[size]
} But I could also live with this public readonly ref struct FixedStruct<T> where T : struct
{
public readonly Span<T> Value;
public FixedStruct(int size)
{
Value = stackalloc byte[size];
}
} It is currently only a proposal which might not have anything to do with reality. My idea was that the stackalloc inside the struct gets part of the structs stacksize, but there might be better solutions. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Duplicate of #89730? |
Beta Was this translation helpful? Give feedback.
Duplicate of #89730?