Indexed members

Topics: C# Language Design
Apr 8, 2014 at 9:23 AM
Hello,
I have noticed the indexed members got into the upcoming features in CSharp. Now there are features I don't look forward to, but what is the rationale behind this one?

Is there anything significant you get with customer.$first instead of customer["first"] I don't see? You even don't save any typing if you have automatic brace completion on...

The cases when you can use it is quite limited, you need to fallback to the [] syntax anytime the string begins with or is a number, contains dot, dash or similar, which results in mixed syntax through the code. The strings are actually treated as identifiers (which changes meaning of @ and some intellisense features), but they do not conflict with other identifiers....

So where are the minus 100 points to begin with? :-)
Apr 8, 2014 at 6:22 PM
There's been some discussion about this feature (whose official name, I believe, is "lightweight dynamic member access") in one of the meeting notes threads: https://roslyn.codeplex.com/discussions/540574

The C# team hasn't answered yet, hopefully they will soon. I have to say that to me it seems like they're so excited about their new toy (Roslyn) and the possibilities it brings that they forgot C# is not a toy language...
Marked as answer by JanKucera on 4/8/2014 at 11:55 AM
Apr 8, 2014 at 6:55 PM
Oh I see, I only saw the Word version. Thanks for the redirect.

I too hope the features coming will be chosen wisely and be justified.
Coordinator
Apr 14, 2014 at 6:38 PM
The feature is for cases where you really do think of string indexing as "poor man's member access", and want to express that intent in the syntax. I like that in itself, but I also think it has a little more potential in the future:
  • We could improve intellisense to show $identifier or something when you press dot on a value that has an indexer that will take a string. This increases discoverability.
  • You could consider custom lightweight type systems over these, in the vein of F# type providers or TypeScript's optional type system. Thus you could get checking, auto-completion and safe refactoring for these guys.
These are just speculation though. Currently this is definitely one of the very small features. But we still like it a lot. :-)
Apr 16, 2014 at 6:30 AM
JanKucera
Is there anything significant you get with customer.$first instead of customer["first"] I don't see?
Asked myself same question.

madst
We could improve intellisense to show $identifier or something when you press dot on a value that has an indexer that will take a string. This increases discoverability.
Why you can't also show "non lightweight" member names? More complicated implementation in intellisense?
You could consider custom lightweight type systems over these, in the vein of F# type providers or TypeScript's optional type system. Thus you could get checking, auto-completion and safe refactoring for these guys.
How is this related with member access syntax? Can you give an example?
Apr 17, 2014 at 1:44 PM
I like the feature, as I do alot of dynamic access in my current app, but it seems a little weak that we only got half of the equation, and the dynamic member access is still required to confine with C# keywords. (E.g. you can't name a property "delegate", you have to call it "@delegate" and you can't name a method starting with $. In my universe $ is what I use to prefix primate members)
I still think that we need a better object literal notation. If there was an easy way to instantiate a new object it would be rather nice.
May 3, 2014 at 4:24 PM
I'm surprised the C# team intends to add this feature. Normally, they are very concerned with not adding redundant cruft that can never ever be taken out of the language.

Also, there are now quite many ways to initialize dictionaries (I count 4 realistic initialization styles). This is confusing to beginners and it fosters messy code with mixed styles from different team members.

This feels like the version where C# jumps the shark and becomes crufty.
May 3, 2014 at 5:47 PM
xor88 wrote:
I'm surprised the C# team intends to add this feature. Normally, they are very concerned with not adding redundant cruft that can never ever be taken out of the language.

Also, there are now quite many ways to initialize dictionaries (I count 4 realistic initialization styles). This is confusing to beginners and it fosters messy code with mixed styles from different team members.

This feels like the version where C# jumps the shark and becomes crufty.
I would agree. In my opinion the only argument one could make for the addition of this feature would be feature parity with VB.NET, which has the ! operator for this purpose. Fortunately it does seem that based on feedback that this feature wouldn't be that worthwhile:

C# Language Design Notes for Apr 21, 2014 (Part I)
Developer
May 3, 2014 at 9:24 PM
OK, this feature is now withdrawn.