Skip to content

Commit 848f521

Browse files
CopilotBillWagner
andcommitted
Fix LineBuffer to be well-behaved collection
Co-authored-by: BillWagner <[email protected]>
1 parent b6e8d6f commit 848f521

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

docs/csharp/language-reference/operators/snippets/shared/CollectionExpressionExamples.cs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
public class LineBuffer : IEnumerable<char>
1111
{
1212
private readonly char[] _buffer = new char[80];
13+
private readonly int _count;
1314

1415
public LineBuffer(ReadOnlySpan<char> buffer)
1516
{
@@ -18,10 +19,30 @@ public LineBuffer(ReadOnlySpan<char> buffer)
1819
{
1920
_buffer[i] = buffer[i];
2021
}
22+
_count = number;
2123
}
2224

23-
public IEnumerator<char> GetEnumerator() => _buffer.AsEnumerable<char>().GetEnumerator();
24-
IEnumerator IEnumerable.GetEnumerator() => _buffer.GetEnumerator();
25+
public int Count => _count;
26+
27+
public char this[int index]
28+
{
29+
get
30+
{
31+
if ((uint)index >= (uint)_count)
32+
throw new IndexOutOfRangeException();
33+
return _buffer[index];
34+
}
35+
}
36+
37+
public IEnumerator<char> GetEnumerator()
38+
{
39+
for (int i = 0; i < _count; i++)
40+
{
41+
yield return _buffer[i];
42+
}
43+
}
44+
45+
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
2546

2647
// etc
2748
}

docs/csharp/language-reference/operators/snippets/shared/operators.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
5-
<TargetFramework>net8.0</TargetFramework>
5+
<TargetFramework>net10.0</TargetFramework>
66
<Nullable>enable</Nullable>
77
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
88
<ImplicitUsings>true</ImplicitUsings>

0 commit comments

Comments
 (0)