ReadOnlySpan<T> 和 IReadOnlyList<T> 之间有没有共同的接口?

Is there any common interface between ReadOnlySpan<T> and IReadOnlyList<T>?

我想知道当涉及 ReadOnlySpan<T>IReadOnlyList<T>(以及更通用的接口)时是否有任何接口、模式或其他内容,并且您希望避免无用分配.

考虑使用此方法IEnumerable<T>,但不介意实际功能:

    public byte Compute(IEnumerable<byte> buffer)
    {
        unchecked
        {
            byte lrc = 0;
            foreach (byte cell in buffer)
            {
                lrc ^= cell; //just an example
            }
            return lrc;
        }
    }

计算是根据字节序列进行的(甚至有时我需要一个 indexed/random-access 流)。因此,序列可以是数组、其中的一部分或任何可枚举的来源。

到目前为止,我无法找到一种合适的方法来概括方法签名(甚至接受一些重载作为转换),而无需实际分配数组或其他东西 "heavy"。

对于即将到来的 .Net Standard 2.1 有什么,甚至计划吗?

到目前为止,这似乎是我找到的最不丑陋的解决方案:

    public byte Compute(IEnumerable<byte> buffer)
    {
        unchecked
        {
            byte lrc = 0;
            foreach (byte cell in buffer)
            {
                this.ComputeCore(ref lrc, cell);
            }
            return lrc;
        }
    }


    public byte Compute(ReadOnlySpan<byte> span)
    {
        unchecked
        {
            byte lrc = 0;
            foreach (byte cell in span)
            {
                this.ComputeCore(ref lrc, cell);
            }
            return lrc;
        }
    }

    private void ComputeCore(ref byte acc, byte cell)
    {
        acc ^= cell;
    }

当然,只有当核心功能变得比描述的复杂一些时,才值得。