MemoryStream.TryLookAhead C# (CSharp) Method

TryLookAhead() public method

public TryLookAhead ( int offset, &item ) : bool
offset int
return bool
    public bool TryLookAhead(int offset, [MaybeNullWhen(false)] out T item)
    {
        var index = this.Index + offset;
        if (index >= this.Memory.Length)
        {
            item = default;
            return false;
        }
        else
        {
            item = this.Memory.Span[index];
            return true;
        }
    }

Usage Example

示例#1
0
    public void BufferedStream()
    {
        var ms = new MemoryStream <int>(new int[] { 1, 2, 3, 4, 5, 6 }.AsMemory()).Filter(n => n % 2 == 1).ToBuffered();

        Assert.False(ms.IsEnd);
        Assert.True(ms.TryPeek(out var t0));
        Assert.Equal(1, t0);
        Assert.True(ms.TryLookAhead(1, out var t1));
        Assert.Equal(3, t1);
        Assert.False(ms.IsEnd);
        Assert.Equal(1, ms.Consume());
        Assert.False(ms.IsEnd);
        Assert.Equal(3, ms.Consume());
        Assert.False(ms.IsEnd);
        Assert.Equal(5, ms.Consume());
        Assert.True(ms.IsEnd);
    }