CS0506: Cannot override GetEnumerator() in subclass of ReadOnlyCollectionBase

D

Dennis Homann

Hi all,

I do understand the need for virtual/override/new and also when to use what.
.... at least I thought so.
Why does the following code result in a CS0506 compiler error (should
translate to sth like "ReadOnlyCollectionBase.GetEnumerator() is not
declared as virtual, abstract, or override")?

public class MyCollection : ReadOnlyCollectionBase {
public override IEnumerator GetEnumerator() {
return null;
}
}

Please ignore the fact that this class is useless. I just tried to create a
minimal example.

According to the documentation and ildasm
System.Collections.ReadOnlyCollection.GetEnumerator() IS declared as
virtual.

Thanks,
Dennis
 
J

Jon Skeet [C# MVP]

Dennis Homann said:
I do understand the need for virtual/override/new and also when to use what.
... at least I thought so.
Why does the following code result in a CS0506 compiler error (should
translate to sth like "ReadOnlyCollectionBase.GetEnumerator() is not
declared as virtual, abstract, or override")?

Because unfortunately the documentation is wrong - it's *not* virtual.

Don't be fooled my ildasm showing the "virtual" modifier - that shows
that it's *called* virtually, not that it *is* virtual, IIRC. All
rather confusing. The important modifier here is "final", which shows
it's not virtual.
 
D

Dennis Homann

Thanks, Jon. It all makes sense now.

- Dennis

Jon Skeet said:
Because unfortunately the documentation is wrong - it's *not* virtual.

Don't be fooled my ildasm showing the "virtual" modifier - that shows
that it's *called* virtually, not that it *is* virtual, IIRC. All
rather confusing. The important modifier here is "final", which shows
it's not virtual.
 

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