Beginner Implement IList problem

P

Paul

Hi all,

I'm trying to implement IList and keep getting an error when trying to
implement GetEnumerator().
My class has a List<String> and I've been using its methods as return
types for IList, but I can't seem to figure the the get enumerator
section. I try:

//IEnumerable
public IEnumerator GetEnumerator()
{

return list.GetEnumerator();
}

but keep getting the error:

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

Any help would be appreciated.

Thanks,

Paul
 
J

Jon Skeet [C# MVP]

Paul said:
I'm trying to implement IList and keep getting an error when trying to
implement GetEnumerator().
My class has a List<String> and I've been using its methods as return
types for IList, but I can't seem to figure the the get enumerator
section. I try:

//IEnumerable
public IEnumerator GetEnumerator()
{

return list.GetEnumerator();
}

but keep getting the error:

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

Are you sure you're trying to implement IList rather than
IList<string>? The error message suggests that you're trying the latter
(which itself derives from IEnumerable<T>) whereas your method
declaration is really implementing IEnumerable (which you need if
you're implementing the non-generic IList). In fact, you'll need the
non-generic version anyway, because IEnumerable<T> extends IEnumerable.
You'll need to implement one of them explicitly, eg:

public IEnumerator<string> GetEnumerator()
{
return list.GetEnumerator();
}

public IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top