C# Memory<T>

I need to store (as a field on a class) a contiguous block of memory. In Xojo, I used a class called a MemoryBlock. It looks like C# provides the Span<T> and Memory<T> structs which do what I need. The Span<T> struct can’t be used as a field for a class on the heap but Memory<T> can. I’m using Elements RTL. I don’t see Memory<T> in the RTL docs. Does that mean it isn’t supported?

Essentially, I need to store bytecode for a VM I’m writing an I thought it would be better performing to store it as a Memory<T> than an array of byte. Does anyone have any suggestions about this?

Which platform are you working on? Because Memory<T> is a .NET type you can just use on Elements too. if it’s island or Toffee, we have Span types that you can use on the heap.

I’m using .NET for the Windows console app and Island for the macOS app.

FWIW, just a Byte[] should work too? with “unsafe” code enabled (in .NET), you can get a pointer to element [0] and then just treat it as a random access more block as you please.

1 Like