OriginalGriff suggested changing what the enumerator returned.
I'd argue against that because it is not the expected semantics of
GetEnumerator()
.
I'd use
object
less and correct types more:
public class List : IEnumerable<ListNode> ...
and
public IEnumerator<ListNode> GetEnumerator() ...
then the summation can be:
foreach (ListNode s in listOfNodes)
sum += (int)s.inData;
or, using Linq:
sum = listOfNodes.Sum(s => (int)s.inData);
And, of course, if
ListNode.inData
will
always be
int
,
then declare it as such and the cast to
int
is unnecessary.