Accessing indexer of base class with identical signature

C

Clive Dixon

Is it possible to access an indexer of a base class with identical
signature, e.g.

class Class1
{
public object this[object o]
{
get
{
// ...
}
}
}

class Class2 : Class1
{
public new object this[object o]
{
get
{
return base.this[o];
}
}
}

The compiler will not accept 'base.this' nor 'base.Item'/'base.get_Item' and
I can't find any reference to calling base class indexers anywhere in the
documentation. Now if the base class indexer has a different signature I can
simply use 'this' rather than 'base.this' and cast the indexer parameter to
that of the base class's indexer to force calling the correct base class
indexer, but if the signatures are identical I cannot see how to call the
base class indexer.
 
N

Nicholas Paldino [.NET/C# MVP]

Clive,

Have you tried using:

return base[o];

Hope this helps.
 
L

Larry Lard

Clive said:
Is it possible to access an indexer of a base class with identical
signature, e.g.

class Class1
{
public object this[object o]

If your method really is called 'this' then I can't even begin to
imagine what the compiler thinks you mean!
class Class2 : Class1
{
public new object this[object o]

Why 'new' when nothing has changed?
 
C

Clive Dixon

Yep thanks Nicholas. Obvious once pointed out, but I just didn't spot it.

Nicholas Paldino said:
Clive,

Have you tried using:

return base[o];

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Clive Dixon said:
Is it possible to access an indexer of a base class with identical
signature, e.g.

class Class1
{
public object this[object o]
{
get
{
// ...
}
}
}

class Class2 : Class1
{
public new object this[object o]
{
get
{
return base.this[o];
}
}
}

The compiler will not accept 'base.this' nor 'base.Item'/'base.get_Item'
and I can't find any reference to calling base class indexers anywhere in
the documentation. Now if the base class indexer has a different
signature I can simply use 'this' rather than 'base.this' and cast the
indexer parameter to that of the base class's indexer to force calling
the correct base class indexer, but if the signatures are identical I
cannot see how to call the base class indexer.
 

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