Skip to content

Fix Collection builder example to be well-behaved with proper buffer allocation #47994

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
public class LineBuffer : IEnumerable<char>
{
private readonly char[] _buffer = new char[80];
private readonly int _count;

public LineBuffer(ReadOnlySpan<char> buffer)
{
Expand All @@ -18,10 +19,30 @@ public LineBuffer(ReadOnlySpan<char> buffer)
{
_buffer[i] = buffer[i];
}
_count = number;
}

public IEnumerator<char> GetEnumerator() => _buffer.AsEnumerable<char>().GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => _buffer.GetEnumerator();
public int Count => _count;

public char this[int index]
{
get
{
if ((uint)index >= (uint)_count)
throw new IndexOutOfRangeException();
return _buffer[index];
}
}

public IEnumerator<char> GetEnumerator()
{
for (int i = 0; i < _count; i++)
{
yield return _buffer[i];
}
}

IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();

// etc
}
Expand Down
Loading