1

Given the following Generic Method:

    /// <summary>
    /// Performs a MoveNext on the IEnumerator 'Enoomerator'. 
    /// If it hits the end of the enumerable list, it resets to the beginning.
    /// </summary>
    /// <returns>If False, MoveNext has failed even after resetting,
    /// which means there are no entries in the list.</returns>
    private static bool CyclicalSafeMoveNext<T>(T Enoomerator) 
                                           where T : IEnumerator<T> 
    {
        if (Enoomerator.MoveNext()) //  successfully moved to the next element
        {
            return true;
        }
        else
        {
            // Either reached last element (so reset to beginning) or
            // trying to enumerate in a list with no entries 
            LogLine("CyclicalSafeMoveNext: failed a MoveNext, resetting.");
            Enoomerator.Reset();

            if (!Enoomerator.MoveNext())
            {
                // Still at end. Zero entries in the list?
                LogLine("CyclicalSafeMoveNext: failed a MoveNext after 
                Reset(), which means there were no entries in the list.");
                return false;
            }
            else
            {
                // Resetting was successful
                return true;
            }
        }
    }

When I compile this code

IEnumerator<FileInfo> FileInfoEnumerator files;                               
while (CyclicalSafeMoveNext(files))
{
    return files.Current.FullName;
}

I get the error:

Error 7 The type 'System.Collections.Generic.IEnumerator<System.IO.FileInfo>' cannot 
be used as type parameter 'T' in the generic type or method
'CyclicalSafeMoveNext<T>(T)'. There is no implicit reference conversion from
'System.Collections.Generic.IEnumerator<System.IO.FileInfo>' to 'System.Collections.Generic.IEnumerator<System.Collections.Generic.IEnumerator<System.IO.FileInfo>>'.

Why am I getting this error and how do I correct my code?

7
  • I really like your Enoomerator :) Commented Jul 21, 2014 at 20:45
  • @Noob, yeah, I'm going to get off SO for today. Commented Jul 21, 2014 at 20:46
  • @quetzalcoatl, note the hidden characters in the error text, which blockquotes don't show Commented Jul 21, 2014 at 20:47
  • You code won't compile for entirely different reasons than the reasons you provided. You need to provide us with a code sample that can accurately replicate your problem. Commented Jul 21, 2014 at 20:47
  • 1
    @gunr2171: Wow. Sorry, you're right. I'm pretty sure that on Preview those were visible. Strange. Hm. I've cheked, not, they were not visible. Thanks! Commented Jul 21, 2014 at 20:47

2 Answers 2

5

One problem is here:

where T : IEnumerator<T> 

You're restricting the generic class to a class that is its own enumerator.

Since FileInfo is not an IEnumerator<FileInfo> and IEnumerator<FileInfo> is not an IEnumerator<IEnumerator<FileInfo>> it fails the generic constraint.

You could add a second generic type:

private static bool CyclicalSafeMoveNext<T, U>(T Enoomerator) 
                                       where T : IEnumerator<U>

or just make IEnumerator<T> part of the signature:

private static bool CyclicalSafeMoveNext<T>(IEnumerator<T> Enoomerator) 
Sign up to request clarification or add additional context in comments.

1 Comment

You're right, that's the signature I needed. Clearly I'm not fluent in Generic Method design yet.. and Thanks!
2

try

private static bool CyclicalSafeMoveNext<T>(IEnumerator<T> Enoomerator)

having the where T : IEnumerator<T> is throwing it off.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.