C++/CLI: Implementing IList and IList (explicit implementation of a default indexer)

c++-cliexplicit-interfaceindexer

I am trying to implement a C++/CLI class that implements both IList and IList<T>.

Since they have overlapping names, I have to implement one of them explicitly, and the natural choice should be IList.

The implicit implementation of the indexer is:

using namespace System::Collections::Generic;
generic<class InnerT> public ref class MyList : public System::Collections::IList, IList<InnerT> {
  // ...
  property InnerT default[int]{
    virtual InnerT get(int index);
    virtual void set(int index, InnerT item);
  }
}

I am now trying to declare the default indexer for IList.

My guess would be something like this:

  property Object^ System::Collections::IList::default[int]{
    virtual Object^ System::Collections::IList::get(int index);
    virtual void System::Collections::IList::set(int index, Object^ item);
  }

but that just gives me

error C2061: syntax error : identifier 'default'

Any hints?

Best Solution

JaredPar's answer almost worked. Two things should be changed:

  • The indexer-property needs a different name since "default" is already taken by the implicit implementation.
  • The specification of the overriding needs to be done on the set- and get-methods, not on the property itself.

I.e.:

  property Object^ IListItems[int]{
    virtual Object^ get(int index) = System::Collections::IList::default::get;
    virtual void set(int index, Object^ item)  = System::Collections::IList::default::set;
  }
Related Question