Blog ( Of Ryan Olshan)

Recent Posts

News

About Ryan Olshan

LinkedIn Profile

Follow Ryan via twitter

Join StrongTypes - Free Generics Help By Email
Join StrongTypes

Community Credit Winner

View my MVP profile

The posts on this weblog are provided "as is" with no warranties and confer no rights. The opinions expressed herin are my own personal opinions and do not represent my employer's view in any way.

Subscribe to Rolog via My AOL
Subscribe to Rolog via Bloglines


Subscribe to Rolog via Feedburner

Furl Rolog




Subscribe to Rolog via Newsburst


Subscribe to Rolog via Pluck

Add Rolog to Technorati Favorites




Thycotic Secret Server

Receive Email Updates


.NET Websites

  • => StrongTypes.com <=
  • Community Credit
  • DotNetCharity.com
  • DotNetSlackers
  • EasyListBox
  • LearnAsp.com
  • SystemNetMail.com
  • SystemWebMail.com

Blogroll

  • Alex Campbell
  • Carlos Magalhaes
  • Charles Carroll
  • Chris Taylor
  • Darren Neimke
  • David Betz
  • Eric Smith
  • Rob Caron
  • Rob Howard
  • Sam Gentile
  • Scott Guthrie
  • Scott Mitchell
  • Tess
  • Wesner Moise
  • Jason Alexander
  • Bill Ryan
  • Steve Smith
  • Sahil Malik
  • David Silverlight

Other Websites

  • 911 Pigeon Alert
  • Pacific Wildlife Project

Strong Coder Bloggers

  • Dean Fiala
  • Doug Nelson
  • Kevin Jones

Yahoo! Groups

  • => StrongTypes <=
  • AspNet2
  • AspNet20GridView
  • PayPalDevDotNet
  • RubyOnRails
  • VSTS

IEnumerable<> and IEnumerable

I've answered this 2x in the past week, so I think it's a worthy point to bring up. When implementing generic interface IEnumerable<>, you'll need to implement both IEnumerable<>.GetEnumerator() and IEnumerable.GetEnumerator() as IEnumerable<> inherits IEnumerable. Otherwise, you'll get the below error message:

'xxx' does not implement interface member 'System.Collections.IEnumerable.GetEnumberator()'. 'xxx' is either static, not public, or has the wrong return type.

As for why IEnumerable<> inherits IEnumerable, the best answer I could come up with is because it can.

So, to sum things up:

using System.Collections;

using System.Collections.Generic;

 

public class MyClass : IEnumerable

{

    public IEnumerable GetEnumerator()

    {

        // Code here

    }

 

    public IEnumerable GetEnumerator()

    {

        // Code here

    }

}

Share this post: | bookmark it! | digg it! | reddit! | kick it! | live it!
Posted: May 08 2006, 12:46 PM by Ryan Olshan | with 5 comment(s)
Filed under:

Comments

ryanjb2 said:

Thanks, that solved the first problem, but now it says that my class already defines GetEnumerator with the same parameter types.
# May 10, 2006 5:45 PM

Ryan Olshan said:

Could you post the relevant code?

Ryan

# May 12, 2006 10:47 AM

ryanjb2 said:

public class OrderPaintItem : PaintItem, IEnumerable
{
private List paintComponents = new List();

......

public IEnumerable GetEnumerator()
{
return this.paintComponents.GetEnumerator();
}

public IEnumerable GetEnumerator()
{
return this.paintComponents.GetEnumerator();
}
}

Sorry if that isn't formatted correctly.

Originally I only implemented "IEnumerable<>.GetEnumerator()", and I got the error you described in your initial blog entry.  Implementing "IEnumerable.GetEnumerator()" got rid of that but now it complains about two indentical methods with the same argument types.  Thank you for any help.

# May 12, 2006 11:07 AM

Ryan Olshan said:

Change it to something like:

public IEnumerable GetEnumerator()
{
    foreach (PaintComponent item in paintComponents)
    {
        yield item;
    }
}

HTH,
Ryan

# May 15, 2006 7:04 AM

ryanjb2 said:

Everything is working fine now.  I did have to use "yeild return item" instead of just "yeild item."

Thank you for your help.  Previous to this, I had a function that would build a DataTable out of the collection for databinding.  That was a big waste of resources and time.

# May 17, 2006 8:15 AM