-1

I'm wondering why this is not working the way I expect it to work. Is this just the matter of "to ship is to choose" or is there a good practical reason, that the behaviour I'm expecting is problematic.

Consider this variable definition:

int[,] a

And this function signature

(int, int) FindIndex(int[,] a)

Given these I would expect this to work:

int index = a[FindIndex(a)];

But it does not, it gives:

CS0022 Wrong number of indices inside []; expected 2

I did not check the spec, but I'm sure that this is in accordance with spec, so I do not question if the implementation is correct, it surely is. What I would like to know, are there any practical reasons and/or considerations for this not to be supported?

Andrew Savinykh
  • 25,351
  • 17
  • 103
  • 158
  • obviously because it's just a syntax sugar for ValueTuple ... and it cannot be indexer of int[,] – Selvin Nov 24 '18 at 01:24
  • 1
    I assume the problem is that most Tuples would in fact be invalid there. Its only the specific example of a `(int,int)` that would make sense and it probably isn't worth all the effort that would be needed to make it work for something that doesn't really add any extra capabilities to the language. – Chris Nov 24 '18 at 01:25
  • @Selvin it sounds a bit naive. you can do `x,y = FindIndex(a)` where `x` and `y` are `int`s despite the fact that it's `ValueTuple`. It cannot be indexer, because it is not supported in the spec. The question is if there is a good reason for that. – Andrew Savinykh Nov 24 '18 at 01:50
  • you mean `(x,y) = FindIndex(a);` which is translated to `ValueTuple valueTuple = FindIndex(a); x = valueTuple.Item1; y = valueTuple.Item2;` ... ins `a[FindIndex(a)];` there is no variables to assign – Selvin Nov 24 '18 at 02:00
  • 1
    [The question is "why does C# not have this feature?" The answer to that question is always the same. Features are unimplemented by default; C# does not have that feature because no one designed, implemented and shipped the feature to customers.](https://stackoverflow.com/a/8673015) – user4003407 Nov 24 '18 at 02:02

1 Answers1

1

Totally nonsensical class to take tuples as an indexer

public class MyFunkyArray<T> : IEnumerable<T>
{
    public MyFunkyArray() { }

    public MyFunkyArray(T[,] buffer) => Buffer = buffer;

    public T[,] Buffer { get; set; }

    public T this[(int, int) tuple]
    {
        get => Buffer[tuple.Item1, tuple.Item2];
        set => Buffer[tuple.Item1, tuple.Item2] = value;
    }

    public IEnumerator<T> GetEnumerator() => ToEnumerable(Buffer).GetEnumerator();

    IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();

    public IEnumerable<T> ToEnumerable(Array target) => target.Cast<T>();
}

Usage

var array = new MyFunkyArray<int>(new int[2, 2]);

var tuple = (1, 1);

array[tuple] = 3;

foreach (var val in array)
    Console.WriteLine(val);

Output

0
0
0
3

Note : this is really only for academic purposes, and has limited value


Additional Resources

Indexers (C# Programming Guide)

Indexers allow instances of a class or struct to be indexed just like arrays. The indexed value can be set or retrieved without explicitly specifying a type or instance member. Indexers resemble properties except that their accessors take parameters.

TheGeneral
  • 79,002
  • 9
  • 103
  • 141