1

This article (which discussed new features in C# 4.0) includes the following text in the 'Improved COM Interop' section:

excelApp.Range[“A1”, “B3”].AutoFormat(
    Excel.XlRangeAutoFormat.xlRangeAutoFormatClassic2);

...this code uses one more new feature: indexed properties (take a closer look at those square brackets after Range.) But this feature is available only for COM interop; you cannot create your own indexed properties in C# 4.0.

My question is: what exactly does "available" mean?

  • One scenario seems to be that you can CONSUME in C# a COM interface which includes indexed properties - is this accurate? It appears to be what is shown in the Excel example. I assume then that COM IDL has a syntax for this?

  • But there is also a second possible meaning - can you EXPORT TO COM a C# class/interface and make it appear to have an indexed property? For instance, by using some attribute to make the COM interface have an indexed property.

There could be other interpretations.

I do understand that you can't create a property in C# itself which naturally accepts indexed notation.

Maybe someone can cite / provide some simple examples which clarify what is/is not possible?


(The above text was also mentioned in another question, but the focus of that question was not the COM-related features, but the lack of general indexed properties in C#. I'm specifically asking about the COM interop aspect of this).

StayOnTarget
  • 11,743
  • 10
  • 52
  • 81
  • "I do understand that you can't create a property in C# itself which naturally accepts indexed notation." -- why would you assume the text means anything other than that, then? – Jeroen Mostert Aug 17 '18 at 12:03
  • If your question is just about how, exactly, COM interop with indexed properties works, you're probably not doing yourself any favors with quoting that text. It's clear the author wasn't interested in explaining exactly how it works, the remark is just a throwaway to remind readers you can't declare them directly in C#. (And since you can't, you can't export them to COM either -- it would be very weird to have syntax to declare things that enable indexed properties on C# classes *but only for COM*. Going the extra inch to make them work in C# generally would be trivial in that case.) – Jeroen Mostert Aug 17 '18 at 12:06
  • 2
    Looks pretty accurate to me as-is, you can consume but not export. The language used to create the COM component's type library was not C#. IDL (Interface Description Language) is the big dog. – Hans Passant Aug 17 '18 at 12:07
  • @mjwills the question specifically stated that it was about the COM interop elements, not the general C# language features. (I've edited to try and make that more explicit if it was not clear enough). – StayOnTarget Aug 17 '18 at 12:11
  • In that case, the answer is `Your first scenario is the correct reading`. – mjwills Aug 17 '18 at 12:11
  • @JeroenMostert because it was specifically talking about COM interop for which there are plenty of special compiler features. – StayOnTarget Aug 17 '18 at 12:11
  • Fwiw, it is not the only COM-specific feature of properties they decided to not implement. In COM it is valid to have the default property (indexer in C# speak) to not have any arguments. And you can define two property setters, propput and propputref. This causes syntax ambiguities and is the basic reason why a VBA programmer has to learn the Set keyword. Hard to learn, so they dropped it. – Hans Passant Aug 17 '18 at 12:18
  • At binary level, Application's Range is just a method, defined, like this in IDL: `HRESULT Range([in] VARIANT Cell1, [in, optional] VARIANT Cell2, [out, retval] Range** RHS)`. Even if it's decorated with attributes that say it's a property, or parameters are optional or return values, this is only "syntactic sugar" for high level languages such as VB/VBA or .NET than a real "property". In machine code, it will always be a method with 3 arguments (VARIANT, VARIANT, pointer) that returns a 32-bit value. – Simon Mourier Aug 17 '18 at 13:21

0 Answers0