using System.Runtime.InteropServices; namespace Vanara.PInvoke { /// /// Enumerator with zero copy access using ref. /// public unsafe ref struct RefEnumerator where T : unmanaged { private T* _arrayPtr; private readonly int _count; private int _index; /// /// Create RefEnumerator. /// /// Pointer to unmanaged array /// Number of elements in the public RefEnumerator(T* arrayPtr, int count) { _arrayPtr = arrayPtr; _count = count; _index = -1; } /// /// Move to next element. /// public bool MoveNext() { int index = _index + 1; if (index < _count) { _index = index; _arrayPtr++; return true; } return false; } /// /// Return current element. /// public ref T Current => ref *_arrayPtr; } }