Override and System.Collections

H

Hoang Do

I have a curious question and I am sure someone else must have run into it
before.
Is it a problem with the collections class or am I misunderstanding the
keyword "Overide".
why does this compile:
----------------------------------------------------
using System;

class test {

abstract class eng

{

public eng(){}

virtual public void calc()

{ Console.WriteLine("Base");}

}

class derivedFrom : eng

{

public derivedFrom(){}

override public void calc()

{ Console.WriteLine("Derived 1");}

}

static void Main() {

derivedFrom xx=new derivedFrom();


xx.calc();

}

}

----------------------------------------------------

and this won't:
----------------------------------------------------
using System;

class test {

class col : System.Collections.CollectionBase {

public col() {}


override public void RemoveAt(int index)

{

Console.WriteLine("RemoveAt");

}


}

static void Main()

{

col x = new col();

col.RemoveAt(0);

}

}

Hoang Do, (e-mail address removed)
 
J

Jon Skeet [C# MVP]

Hoang Do said:
I have a curious question and I am sure someone else must have run into it
before.
Is it a problem with the collections class or am I misunderstanding the
keyword "Overide".

The docs are incorrect - CollectionBase.RemoveAt isn't actually
virtual.
 
A

Adam Clauss

Gotta wonder if the docs are right and the code is wrong (yes, I know the docs should always follow the code)... it seems like it
*should* be virtual. Or is there a particular reason why it wouldn't be?
 
J

Jon Skeet [C# MVP]

Adam Clauss said:
Gotta wonder if the docs are right and the code is wrong (yes, I know
the docs should always follow the code)... it seems like it
*should* be virtual. Or is there a particular reason why it wouldn't
be?

I'm not sure I see why it really should be virtual. CollectionBase is
generally for strongly-typed collections. Why would you want to
override CollectionBase.RemoveAt?
 
A

Adam Clauss

Maybe I'm missing at what level CollectionBase is.

CollectionBase does not specify what TYPE of collection is used does it (array, linked list, etc)? Or if it does, that is where I
was mistaken.

For example I might want to do something different if removing from an array than removing from a linked list.
 
J

Jon Skeet [C# MVP]

Adam Clauss said:
Maybe I'm missing at what level CollectionBase is.

CollectionBase does not specify what TYPE of collection is used does
it (array, linked list, etc)? Or if it does, that is where I
was mistaken.

Well, InnerList returns an ArrayList. I infer from that that it's
basically an ArrayList underneath.
For example I might want to do something different if removing from
an array than removing from a linked list.

CollectionBase provides the implementation of the list part - in other
words, you can't decide to make it a linked list if it's not already.
 

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