Skip to main content

Implement custom enumerator

PropertyValue
IdRR0210
Applicable Syntaxclass that implements IEnumerable<T>
Syntax Spanidentifier
Enabled by Default

Usage

Example #1

before.cs
using System;
using System.Collections;
using System.Collections.Generic;

class C<T> : IEnumerable<T>
{
IEnumerator<T> IEnumerable<T>.GetEnumerator()
{
throw new NotImplementedException();
}

IEnumerator IEnumerable.GetEnumerator()
{
throw new NotImplementedException();
}
}
after.cs
using System;
using System.Collections;
using System.Collections.Generic;

class C<T> : IEnumerable<T>
{
IEnumerator<T> IEnumerable<T>.GetEnumerator()
{
throw new NotImplementedException();
}

IEnumerator IEnumerable.GetEnumerator()
{
throw new NotImplementedException();
}

public Enumerator GetEnumerator()
{
return new Enumerator(this);
}

public struct Enumerator
{
private readonly C<T> _c;
private int _index;

internal Enumerator(C<T> c)
{
_c = c;
_index = -1;
}

public T Current
{
get
{
throw new NotImplementedException();
}
}

public bool MoveNext()
{
throw new NotImplementedException();
}

public void Reset()
{
_index = -1;
throw new NotImplementedException();
}

public override bool Equals(object obj)
{
throw new NotSupportedException();
}

public override int GetHashCode()
{
throw new NotSupportedException();
}
}

//TODO: IEnumerable.GetEnumerator() and IEnumerable<T>.GetEnumerator() should return instance of EnumeratorImpl.
private class EnumeratorImpl : IEnumerator<T>
{
private Enumerator _e;

internal EnumeratorImpl(C<T> c)
{
_e = new Enumerator(c);
}

public T Current
{
get
{
return _e.Current;
}
}

object IEnumerator.Current
{
get
{
return _e.Current;
}
}

public bool MoveNext()
{
return _e.MoveNext();
}

void IEnumerator.Reset()
{
_e.Reset();
}

void IDisposable.Dispose()
{
}
}
}

Configuration

roslynator_refactoring.implement_custom_enumerator.enabled = true|false