I am using .NET 6.0 on Windows 10 with Visual Studio 2022 last Version, last Build, and this code runs fine and even SEEMINLGY does what I want: have a look:
But: keep a closer look on the "Init(in ReadonlySpan<Char> a)
"
I can assign my new(a) to the nodes[0], no error or whatever. But when I do, the nodes[0] has STILL garbage data in and I cannot store my type in that [0] field of the stackallocated span field?
Is this a bug in .NET 6.0 or am I doing smth bad/wrong?
Pls help!
Ok here the exact code:
public ref struct MyType
{
private Span<RefType> nodes = stackalloc RefType[30];
public unsafe struct RefType
{
public char* data;
public int length;
public RefType(in CharSpan a)
{
//AsRef(span) is a private method, which gives me back a valid pointer to the span, this works ok in my code! dont bother to much with this
data = Unsafe.AsPointer(ref GetRefTo(s))
length = a.Length;
}
}
public unsafe MyType()
{
nodes.Clear();
}
public unsafe void Init(in ReadOnlySpan<char> a)
{
//nodes[0] = new(a); //this does not work!
//this does not work aswell!
nodes[0].data = Unsafe.AsPointer(ref MemoryMarshal.GetRef(a));
nodes[0].length = a.Length;
Console.WriteLine(*nodes[0].data + " " +
nodes[0].length);
}
public string DoWork()
{
//Thread.Sleep(5000);
return nodes[0].ToString();
}
}