1

I cannot figure out how to correctly do this. I am getting this error on the last line of my code. The error says:

"Cannot implicitly convert type 'System.Collections.Generic.List to 'System.Collections.Generic.IEnumerator'. An explicit conversion exists (Are you missing a cast?)

Here's the code for the function:

public IEnumerator<string> AddElementsAttributesRemoveElementsAttributes()
        {
            List<string> retval = new List<string>();

            try
            {

                    success = false;
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
            return retval;

        }

How could I fix this? Do I need to somehow cast the return value to a different type?

2
  • 6
    Why do you need to return it as IEnumerator<string> and not, say, IEnumerable<string> ? Commented Jul 21, 2014 at 13:46
  • Is it me or does "An explicit conversion exists" not really help at all here? Commented Jul 21, 2014 at 13:58

3 Answers 3

8

To actually return the list as IEnumerator<string> do this:

return retval.GetEnumerator();

However, it would (probably) make much more sense to return it as IEnumerable<string> in which case you have to change the method signature to specify IEnumerable instead of IEnumerator.

An IEnumerator can only be iterated over once, whereas an IEnumerable can be iterated over more than once, which is why I think you probably want the latter. But if not, change your return statement.

Sign up to request clarification or add additional context in comments.

3 Comments

IEnumerator<T> has a Reset method, so it can actually be iterated any number of times. It's also worth noting that certain Enumerable objects create enumerators that cause side effects, and so effectively you cannot use them to create multiple enumerators.
The typical implementation of Reset is to do nothing, so I wouldn't rely on it. It may work for a list, but in general, not so much.
Sure, and most of those same sources are the same sources are IEnumerable objects that can't create multiple IEnumerator objects. In the general case you can't rely on being able to get multiple enumerators from a single enumerable either. They're both single iteration if you don't know something specific about the object.
3

List<T> implements IEnumerable<T>, but not IEnumerator<T>. An Enumerable can create an Enumerator, it is not one.

While you could ask the List for an enumerator from it, and return that, doing so is almost certainly not what you want to do. It's idiomatic throughout C# to pass around Enumerable objects instead of Enumerator objects and to only use the Enumerator at the last possible moment, generally letting a foreach loop do the actual iterating for you. The fact that a foreach loop can accept an IEnumerable and not an IEnumerator is the primary reason for this practice.

If you change the return type of the method to IEnumerable<string> then the return type will be a type your List<string> implements, so it will be implicitly convertible and there will be no other changes within this method needed.

1 Comment

+1 Good explanation. Indeed I missed that, ending up with downvotes!
1

You may try this:

public IEnumerable<string> AddElementsAttributesRemoveElementsAttributes()

instead of

public IEnumerator<string> AddElementsAttributesRemoveElementsAttributes()

You can iterate over the IEnumerable number of times however it is not possible to do so using the IEnumerator

1 Comment

IEnumerator<T> has a Reset method, so it can actually be iterated any number of times. It's also worth noting that certain Enumerable objects create enumerators that cause side effects, and so effectively you cannot use them to create multiple enumerators. So while the situation you describe can be true in certain situations, it's not true in general, and specifically not true here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.