Is there a way to explicityly implement an interfaces indexer? I need to
create a collection class, that implements IList, ICollection, and
IEnumerable. I'd like to create a custom indexer, with a return value of my
choosing, rather than object. Whenever I try, though, the compiler spits out
an error (I use C#, BTW). I've tried simply implementing object this[int
index] in addition to MyClass this[int index], but got an error. I also
tried IList.this[int index] in addition to MyClass this[int index] but also
got an error. How does one explicityly implement the indexer of an interface
so you can implement a custom one? Thanks for the help.

Jon Rista

Re: Explicit interface indexer implementations? by Jon

Jon
Fri Aug 29 02:01:33 CDT 2003

Jon Rista <jrista@hotmail.com> wrote:
> Is there a way to explicityly implement an interfaces indexer? I need to
> create a collection class, that implements IList, ICollection, and
> IEnumerable. I'd like to create a custom indexer, with a return value of my
> choosing, rather than object. Whenever I try, though, the compiler spits out
> an error (I use C#, BTW). I've tried simply implementing object this[int
> index] in addition to MyClass this[int index], but got an error. I also
> tried IList.this[int index] in addition to MyClass this[int index] but also
> got an error. How does one explicityly implement the indexer of an interface
> so you can implement a custom one? Thanks for the help.

It works fine for me:

using System;

interface Foo
{
string this[int i]
{
get;
}
}

class Test : Foo
{
string Foo.this[int i]
{
get { return "interface"; }
}

int this[int i]
{
get { return 1; }
}


static void Main(string[] args)
{
Test t = new Test();
Console.WriteLine (t[0]);
Foo f = t;
Console.WriteLine (f[0]);
}
}

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too