2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Unity C#】List を Span に変換

Posted at

Unityだと CollectionsMarshal.AsSpan メソッドが使えないっぽいので、
他の人の二番煎じですが、Unsafe クラスを使って拡張メソッドを定義しました。

using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;

public static class SpanUtils
{
    /// <summary>
    /// リストの内部配列を Span として取得します。<br/>
    /// 一時的に、かつ節度を持って使用してください。<br/>
    /// </summary>
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static ReadOnlySpan<T> AsSpanUnsafe<T>(this List<T> list)
        => Unsafe.As<DummyList<T>>(list).Items.AsSpan(0, list.Count);

    private class DummyList<T> { public T[] Items; }
}
2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?