I am looking at the Roslyn September 2012 CTP with Reflector, and I noticed that the ChildSyntaxList struct has the following:
public struct ChildSyntaxList : IEnumerable<SyntaxNodeOrToken>
{
private readonly SyntaxNode node;
private readonly int count;
public Enumerator GetEnumerator()
{
return node == null ? new Enumerator() : new Enumerator(node, count);
}
IEnumerator<SyntaxNodeOrToken> IEnumerable<SyntaxNodeOrToken>.GetEnumerator()
{
return node == null
? SpecializedCollections.EmptyEnumerator<SyntaxNodeOrToken>()
: new EnumeratorImpl(node, count);
}
IEnumerator IEnumerable.GetEnumerator()
{
return node == null
? SpecializedCollections.EmptyEnumerator<SyntaxNodeOrToken>()
: new EnumeratorImpl(node, count);
}
public struct Enumerator
{
internal Enumerator(SyntaxNode node, int count)
{
/* logic */
}
public SyntaxNodeOrToken Current { get { /* logic */ } }
public bool MoveNext()
{
/* logic */
}
public void Reset()
{
/* logic */
}
}
private class EnumeratorImpl : IEnumerator<SyntaxNodeOrToken>
{
private Enumerator enumerator;
internal EnumeratorImpl(SyntaxNode node, int count)
{
enumerator = new Enumerator(node, count);
}
public SyntaxNodeOrToken Current { get { return enumerator.Current; } }
object IEnumerator.Current { get { return enumerator.Current; } }
public void Dispose()
{
}
public bool MoveNext()
{
return enumerator.MoveNext();
}
public void Reset()
{
enumerator.Reset();
}
}
}
That is, there is a GetEnumerator
method which returns a struct.
It looks like that
- using a struct is a performance gain similar to the BCL
List<T>.Enumerator
struct, as noted in this answer, and that - the struct does not implement
IDisposable
so as to not have to worry about bugs that could arise from doing so, as noted on Eric Lippert's blog.
However, unlike the BCL List<T>
class, there is a nested EnumeratorImpl
class. Is the purpose of this to
- avoid having a disposable struct, and
- avoid boxing within the explicitly implemented
IEnumerable<SyntaxNodeOrToken>.GetEnumerator
andIEnumerable.GetEnumerator
methods?
Are there any other reasons?