I keep seeing List derived classes that look something like this
class MyClassList : List<MyClass>
{
public MyClass this[int index]
{
get { return (MyClass)base[index]; }
}
}
What is the point of this inheritance? It looks like it just restates the casting of the member. I could understand other types of indexer, but this is just a restatement of the default List indexer, and provokes a Visual Studio warning RE: hiding of the base indexer. Is this the right or wrong thing to do, and why?
There is no good reason for this. It hides the base indexer instead of overriding it, which could be dangerous, and it doesn’t have any effect at all.
In most cases, it’s best just to use a
List<MyClass>directly. There’s no need to create a special class for this if you’re not planning to extendList<>‘s functionality at all.