Creating a COM indexed property from C#?

You can adorn a regular indexer with the IndexerNameAttribute attribute to expose a name for it to other languages. I'm not sure if this will achieve your goal, though.

Unfortunately, C# only supports the calling of named indexers as part of COM interop, there is no supported language way of implementing your own (i.e., a class can only have the default indexer with an IndexerNameAttribute attribute).

You can create something that looks similar for C# callers by implementing a type with an indexer and then having a property of that type, but it doesn't map exactly to the VB6 equivalent you need.

See also: Using Indexers (C#)

Aside
As has been mentioned in other answers, while C# doesn't support named indexers, the .NET CLR and some other languages, such as VB.NET, do. You may want to consider changing your target language in order to get this feature.


According to http://blogs.msdn.com/b/kirillosenkov/archive/2009/10/20/indexed-properties-in-c-4-0.aspx you can't declare indexed properties in C#. However, in contrast to what some of the other answers state, the CLR does support them, and you can declare them in VB.NET.


Named parametrised properties cannot be created in C# (only a single default one, called this is available).

There are a number of options:

  • Change the interface (but that misses the point as client code will need to change).
  • Use VB (.net), which can create such properties.
  • Create an adapter in C++ to give complete control at a COM level.

The first would mean changing the interface, which breaks your requirement. The final option gives the most control but is significantly more complex (unless you already know C++ COM development). I would go with VB.NET.