IEnumerator for generics.
I have several 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 use generics
0
source to share
2 answers
You have not specified the type to return from the enumerator. but I'm going to guess based on the names RTRatePoint and CurvePoint. I would change the code as follows
class Curve: IEnumerable<RTRatePoint> {
IEnumerator<RTRatePoint> IEnumerable<RTRatePoint>.GetEnumerator() {
return GetEnumerator();
}
public RTRatePointEnumerator GetEnumerator() {
return new RTRatePointEnumerator(_hash);
}
}
class CurvePointEnumerator : IEnumerator<CurvePoint>
One element that might tip you off is that IEnumerator <T> additionally implements IDisposable, so CurvePointEnumerator and RTRatePointEnumerator will need to add a Dispose method. Probably though this method can be pretty empty. The reason is that you did not dispose of anything before, now there is no need.
void IDispose.Dispose() {
// Nothing to see here
}
+1
source to share