C# can now consume indexed properties

Kirill Osenkov provides some details on this feature that was added in Beta 2 of C#4.0. I must admit a certain ambivalence toward this feature. On the one hand, this feature will make working with Office COM APIs even easier (as Kirill shows). On the other hand, I fear a rising chorus asking to implement defining indexed properties in C#. That would be a mistake. It would end muddling the waters of what objects ‘owned’ other objects and properties. 

foo.SomeProperty[3]

could represent two very different object models. Is SomeProperty a type that implements an indexer? Or is SomeProperty a property of foo that requires an index? That starts to matter quite a bit when you wonder about LINQ queries and other ways to access whatever data is behind SomeProperty.

I’m close to thinking that I’d have preferred this change wasn’t made. It’s a bit troubling to think that the language accepted changes solely to support interop with COM style APIs. But as long as the indexed properties remain in a box, and are only used for that particular scenario, it won’t be a serious concern.

Created: 11/1/2009 7:39:35 PM

Current Projects

I create content for .NET Core. My work appears in the .NET Core documentation site. I'm primarily responsible for the section that will help you learn C#.

All of these projects are Open Source (using the Creative Commons license for content, and the MIT license for code). If you would like to contribute, visit our GitHub Repository. Or, if you have questions, comments, or ideas for improvement, please create an issue for us.

I'm also the president of Humanitarian Toolbox. We build Open Source software that supports Humanitarian Disaster Relief efforts. We'd appreciate any help you can give to our projects. Look at our GitHub home page to see a list of our current projects. See what interests you, and dive in.

Or, if you have a group of volunteers, talk to us about hosting a codeathon event.