views:

256

answers:

2

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

A: 

IEnumerable<T> and IEnumerator<T>

gah, it swallowed my code

PlacidBox
does the rest of the code stay the same?
ooo
+1  A: 

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
}
JaredPar
what would i put in the dispose method here . .
ooo
Unless you've actually *got* a finalizer, you don't need to put a call to SuppressFinalize.
Jon Skeet
Good call. Fixed
JaredPar