Question

i have a few classes that i am trying to move to using generics

Class1: Curve

this class has the following code:

public class Curve : IEnumerable

  IEnumerator IEnumerable.GetEnumerator()
    {
        return GetEnumerator(); // Calls non-interface method
    }

  public RTRatePointEnumerator GetEnumerator()
    {
        return new RTRatePointEnumerator(_hash);
    }

Class 2:

  public class CurvePointEnumerator : IEnumerator

what is the recommended conversion of these two classes to using generics

Was it helpful?

Solution

You didn't specify the type being returned from the enumerator. but I'm going to guess based on the names it's RTRatePoint and CurvePoint. I would change the code to be the following

class Curve: IEnumerable<RTRatePoint> {
  IEnumerator<RTRatePoint> IEnumerable<RTRatePoint>.GetEnumerator() { 
    return GetEnumerator();
  }
  public RTRatePointEnumerator GetEnumerator() {
    return new RTRatePointEnumerator(_hash);
  }
}

class CurvePointEnumerator : IEnumerator<CurvePoint>

One item that may trip you up is that IEnumerator<T> additionally implements IDisposable so CurvePointEnumerator and RTRatePointEnumerator will need to have a Dispose method added. Likely though this method can be pretty much empty. Reason being if you weren't disposing anything before, there's no need to now.

void IDispose.Dispose() {
  // Nothing to see here
}

OTHER TIPS

IEnumerable<T> and IEnumerator<T>

gah, it swallowed my code

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top