0

I am intrested in creating custom indexer for char[,]. It seems to me that it is prohibited, but I am wondering if there is an oportunity. The code that could have solved the problem is:

public static class GeneratorHelpers
    {
        public static char int[Vector2D position] (this char[,] field)
        {
            return field[position.X, position.Y];
        }
    }

The above code does not compile.

  • Give it a try, compiler is your friend. – Tanveer Badar Aug 02 '21 at 17:28
  • 1
    Does this answer your question? [C# extend indexer?](https://stackoverflow.com/questions/7286181/c-sharp-extend-indexer) and [How to use indexers with Extension Methods having out parameter and function calls](https://stackoverflow.com/questions/3117351/how-to-use-indexers-with-extension-methods-having-out-parameter-and-function-cal) and [Extension indexer for DataSet](https://stackoverflow.com/questions/22477822/extension-indexer-for-dataset) and [Does C# have extension properties?](https://stackoverflow.com/questions/619033/does-c-sharp-have-extension-properties) –  Aug 02 '21 at 17:29
  • @OliverRogier Thank you, they completely answer my question – Gregory Minakov Aug 02 '21 at 18:27
  • @TanveerBadar the code is incorrect and was not expected to work. I've edited question in order to make it clear. – Gregory Minakov Aug 02 '21 at 18:33

1 Answers1

1

Currently, there is no such things as extension properties, operators or indexers in the C# language. The most common pattern for this would be just to take additional parameters to an extension method.

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445