Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / July 2007

Tip: Looking for answers? Try searching our database.

Why this simple generic code does not compile?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Ympostor - 26 Jul 2007 15:44 GMT
public class TestGenericEnumerator<T> :
             System.Collections.Generic.IEnumerable<T>
{
  public System.Collections.Generic.IEnumerator<T> GetEnumerator()
  {
    return null;
  }
}

I don't understand why this is returning:

Error    6    'TestGenericEnumerator<T>' does not implement interface member
'System.Collections.Generic.IEnumerable<T>.GetEnumerator()'.
'TestGenericEnumerator<T>.GetEnumerator()' is either static, not public,
or has the wrong return type.   

Anyone? :(
Peter Morris - 26 Jul 2007 15:51 GMT
>   public System.Collections.Generic.IEnumerator<T> GetEnumerator()

Try this

public System.Collections.Generic.IEnumerator<T> GetEnumerator<T>()
Marc Gravell - 26 Jul 2007 16:08 GMT
> Try this
> public System.Collections.Generic.IEnumerator<T> GetEnumerator<T>()

The T in this method is now a different T to the class's template T
(which the compiler issues a CSO693 warning about) - but this means
that the above doesn't implement the interface, since you could use:

foreach(int i in new
TestGenericEnumerator<string>().GetEnumerator<int>()) {...}

which doesn't do the job of implementing IEnumerable<string>. Good
idea, though.

Marc
Marc Gravell - 26 Jul 2007 15:51 GMT
Because IEnumerable<T> : IEnumerable, and you haven't implemented the
non-generic form; just add:

System.Collections.IEnumerator
System.Collections.IEnumerable.GetEnumerator() {
   return GetEnumerator();
}
Ympostor - 26 Jul 2007 16:45 GMT
Marc Gravell escribió:
> Because IEnumerable<T> : IEnumerable, and you haven't implemented the
> non-generic form; just add:
[quoted text clipped - 3 lines]
>     return GetEnumerator();
> }

Thanks! Now it works, but, now I don't understand why I need this cast:

public class TestGenericEnumerator<T> :
             System.Collections.Generic.IEnumerable<T>
{
  System.Collections.Generic.IEnumerator<T>
    System.Collections.Generic.IEnumerable<T>.GetEnumerator()
  {
    return null;
  }

  System.Collections.IEnumerator
    System.Collections.IEnumerable.GetEnumerator()
  {
    return (

            //why is this cast needed!?
            (IEnumerable<T>)
            //end cast

            this).GetEnumerator();
  }
}

Thanks in advance.
Marc Gravell - 26 Jul 2007 21:20 GMT
> why is this cast needed!?

Because you switched the generic version to an explicit
implementation, and so there is no this.GetEnumerator()

If you want to do this, consider having a single private
implementation that both the generic and non-generic versions invoke:

   private IEnumerator<T> GetEnumerator() {
       return null; // the actual implementation
   }
   IEnumerator<T> IEnumerable<T>.GetEnumerator() {
       return GetEnumerator();
   }
   IEnumerator IEnumerable.GetEnumerator() {
       return GetEnumerator();
   }

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.