Skip to content

Commit 71676f1

Browse files
committed
inner struct ColumnCollection.Enumerator
1 parent 1aaeb03 commit 71676f1

File tree

1 file changed

+47
-2
lines changed

1 file changed

+47
-2
lines changed

Orm/Xtensive.Orm/Orm/Rse/ColumnCollection.cs

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,46 @@ namespace Xtensive.Orm.Rse
1919
[Serializable]
2020
public sealed class ColumnCollection : IReadOnlyList<Column>
2121
{
22+
public struct Enumerator : IEnumerator<Column>, IEnumerator
23+
{
24+
private readonly IReadOnlyList<Column> list;
25+
private int index;
26+
public Column Current { get; private set; }
27+
28+
object IEnumerator.Current =>
29+
index == 0 || index == list.Count + 1
30+
? throw new InvalidOperationException("Enumeration cannot happen")
31+
: Current;
32+
33+
public void Dispose()
34+
{
35+
}
36+
37+
public bool MoveNext()
38+
{
39+
if (index < list.Count) {
40+
Current = list[index++];
41+
return true;
42+
}
43+
index = list.Count + 1;
44+
Current = default;
45+
return false;
46+
}
47+
48+
void IEnumerator.Reset()
49+
{
50+
index = 0;
51+
Current = default;
52+
}
53+
54+
internal Enumerator(IReadOnlyList<Column> list)
55+
{
56+
this.list = list;
57+
index = 0;
58+
Current = default;
59+
}
60+
}
61+
2262
private readonly Dictionary<string, int> nameIndex;
2363
private readonly IReadOnlyList<Column> columns;
2464

@@ -66,8 +106,13 @@ public ColumnCollection Alias(string alias)
66106
/// <summary>
67107
/// Returns an enumerator that iterates through the <see href="ColumnCollection"/>.
68108
/// </summary>
69-
public IEnumerator<Column> GetEnumerator() => columns.GetEnumerator();
70-
109+
public Enumerator GetEnumerator() => new Enumerator(this);
110+
111+
/// <summary>
112+
/// Returns an enumerator that iterates through the <see href="ColumnCollection"/>.
113+
/// </summary>
114+
IEnumerator<Column> IEnumerable<Column>.GetEnumerator() => GetEnumerator();
115+
71116
/// <inheritdoc/>
72117
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
73118

0 commit comments

Comments
 (0)