4

I would like to write an extension for DataRow indexer so I can do something like this:

MyCustomClass myClass;
DataRow[myClass] = 5;

I found this related question: C# extend indexer?

However the question was asked 7 years ago and maybe we can extend indexers now?

Also in the C# 8 sneak preview they mentioned "Extension Everything"

For version 8 some features are in prototype status, some are just concepts. The first four features that should make it to version 8 of C# are:

* Nullable Reference Types
* Async Streams
* Default Interface Methods
* Extension Everything

Maybe I should wait for C# 8 to hit?

Jenny
  • 572
  • 4
  • 16
  • 2
    No, you can't write extension indexers yet. You *could* write an implicit conversion from `MyCustomClass` to `String` or `int` and use the existing indexers, but it's not really clear whether that's a good idea, based only on what you've shown. – Jon Skeet Feb 25 '18 at 08:17
  • Just write regular extension method, like `DataRow.Set(myClass, 5)`. Why it's so important for it to be indexer? – Evk Feb 25 '18 at 10:31
  • @Evk Why have operators at all? Why not use `1.Add(2.Multiply(3))` instead of `1+2*3`? Indexers (and other operators) convey implied meaning that must be learned (or validated) with named methods when used properly. – NetMage Jun 28 '19 at 00:03

0 Answers0