C#C
C#3y ago
Rotor

Implementing IEnumerable<T>

Hi all!

I'm trying to simplify a complex return type that I have to repeat in a lot of places

Result<IEnumerable<Result<TRow, Faults<TDataFault>>>, Faults<TMetadatFault>>

Although I'm pleased with how the type allows me to easily package and aggregate errors that I encounter without having to throw and catch them, the length of the type does no favours to the code's readability.

I sought to start to simplify the type a bit by contracting
IEnumerable<Result<TRow, Faults<TDataFault>>>
down to
IRowSeq<TRow, TDataFault>

Which I tried to implement using the following code
internal interface IRowSeq<TRow, TDataFault>
    : IEnumerable<Result<TRow, Faults<TDataFault>>>
    where TRow : IRowType
    where TDataFault : Enum
{

    public new IEnumerator<Result<TRow, Faults<TDataFault>>> GetEnumerator();

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


Although the code compiles, once I started to update the code I use to generate an IEnumerable
    private static IRowSeq<TRow, TDataFault> GetRows(ExcelData excelData)
    {
        ulong rowNumber = 1;
        
        foreach (var row in excelData.Rows.Skip(1)) // By this point in the code the header has been confirmed
        {
            yield return ParseAndValidateCells(excelData, row, rowNumber);

            rowNumber++;
        }
    }


Then I hit csharp(CS1624)
  • the body of (method) cannot be an iterator block because 'IRowSeq<TRow, TDataFault>' is not an iterator interface type
erhaps I'm looking at this problem from the wrong angle. Could somebody please set me right?
Was this page helpful?