byte[] bytes = new byte[100]; ReadOnlySpanspan = MemoryMarshal.CreateSpan(ref bytes[0], bytes.Length);
byte[] bytes = new byte[100]; Spanspan = MemoryMarshal.CreateSpan(ref bytes[0], 50);
struct MyStruct { public int X; public int Y; } MyStruct[] array = { new MyStruct { X = 1, Y = 2 }, new MyStruct { X = 3, Y = 4 } }; ReadOnlySpanThis example creates a read-only span over an array of struct objects. The span is created using the CreateSpan method, which takes a reference to the first element of the array and the length of the array. Overall, MemoryMarshal CreateSpan is a useful method in C# when working with memory efficiently and safely. Its examples above help address different scenarios when using the class.span = MemoryMarshal.CreateSpan(ref array[0], array.Length);