CollectionBase.GetEnumerator() virtual or not??

4

4Space

Am I losing my mind?

I'm creating a strongly typed collection derived from CollectionBase,
like yay:

public class TestCol : CollectionBase
{
public override IEnumerator GetEnumerator()
{
return null;
}
}

I've done this a thousand times before so I'm sure I'm doing something
very silly. This gets an error:

C:\Temp\WindowsControlLibrary1\WindowsControlLibrary1\TestCol.cs(18):
'WindowsControlLibrary1.TestCol.GetEnumerator()' : cannot override
inherited member 'System.Collections.CollectionBase.GetEnumerator()'
because it is not marked virtual, abstract, or override


Now the MSDN docs say that it is virtual:

public virtual IEnumerator GetEnumerator();

so what am I missing? Aside from a few marbles.


Cheers,

4Space
 
J

Jon Skeet [C# MVP]

4Space said:
Am I losing my mind?

I'm creating a strongly typed collection derived from CollectionBase,
like yay:

public class TestCol : CollectionBase
{
public override IEnumerator GetEnumerator()
{
return null;
}
}

I've done this a thousand times before so I'm sure I'm doing something
very silly.

I don't think you've done the above before, to be honest.
This gets an error:

C:\Temp\WindowsControlLibrary1\WindowsControlLibrary1\TestCol.cs(18):
'WindowsControlLibrary1.TestCol.GetEnumerator()' : cannot override
inherited member 'System.Collections.CollectionBase.GetEnumerator()'
because it is not marked virtual, abstract, or override


Now the MSDN docs say that it is virtual:

public virtual IEnumerator GetEnumerator();

so what am I missing? Aside from a few marbles.

The MSDN is wrong, I'm afraid - it isn't virtual.
 
4

4Space

Jon Skeet [C# MVP] wrote:

The MSDN is wrong, I'm afraid - it isn't virtual.

Aar I see, it IS virtual but also final.

I wonder if I new'd it in the past. hmmmm
 
J

Jon Skeet [C# MVP]

4Space said:
Aar I see, it IS virtual but also final.

I wonder if I new'd it in the past. hmmmm

Well, that depends what you mean by "it IS virtual". It's not virtual
in C# terminology, but it's virtual in CLR terminology - it's called
virtually, but it can't be overridden. It has to be called virtually
because it's an interface method.

An example is probably a good thing here:

using System;

public class Test : ICloneable
{
static void Main()
{
}

public object Clone()
{
return null;
}
}

Look at Test in ILDASM - Clone comes up as virtual, but it's clearly
not virtual in C# terminology.
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

That's something curious, I used the object browser and it says that it's
virtual:
public virtual new System.Collections.IEnumerator GetEnumerator ( )

Member of System.Collections.CollectionBase


But when I tried to override it on a derived class it gave me the same
error.

Cheers,
 

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