thrace

I'm probably missing something here, because my all of my searches came up with nothing.

It seems only natural for Array to have an indexer. But instead, we have to use Get/Set Value().

Why is that



Re: .NET Base Class Library Why doesn't System.Array have an indexer?

Gabriel Lozano-Moran

Who says that arrays don't have indexers Can you post sample code where you are forced to use the Get and SetValue methods




Re: .NET Base Class Library Why doesn't System.Array have an indexer?

thrace

Here's an example:

using System;

class Program
{
    public static void Main()
    {
        Array ints = Array.CreateInstance(typeof(int), 10);
        for (int i=0; i < ints.Length; i++)
            Console.WriteLine(ints[ i ]);
    }
};

error CS0021: Cannot apply indexing with [] to an expr ession of type 'System.Array'

Now obviously, I could use 'int[]', or 'object[]', but that's not the point.

Edit: separated the word 'expr ession' because it confuses the board.





Re: .NET Base Class Library Why doesn't System.Array have an indexer?

VikasGoyal

Hi,

instead of using Array class its recommended to use array contructs provided by the language which should provide you indexer feature also.

http://DotNetWithMe.blogspot.com
vikas goyal






Re: .NET Base Class Library Why doesn't System.Array have an indexer?

thrace

I'd like to understand .NET better by understanding why the Array class wasn't provided with an indexer. It must be have been a conscious design, and not some accidental omission.





Re: .NET Base Class Library Why doesn't System.Array have an indexer?

Mattias Sjogren

The "indexer" is provided by each concrete array type (derived from System.Array), and not System.Array itself. That way the indexer can be strongly typed (Array would have to return Object) and take the appropriate number of index parameters.






Re: .NET Base Class Library Why doesn't System.Array have an indexer?

thrace

Allow me to nag some more: why doesn't Array have an indexer that gets and sets an Object

I realize this matter is insignificant, but .NET internals intrigue me.





Re: .NET Base Class Library Why doesn't System.Array have an indexer?

Mattias Sjogren

thrace wrote:

Allow me to nag some more: why doesn't Array have an indexer that gets and sets an Object

How many index parameters would it take, considering that it has to support everything from one dimensional to N-dimensional arrays






Re: .NET Base Class Library Why doesn't System.Array have an indexer?

thrace

Mattias Sjogren wrote:
How many index parameters would it take, considering that it has to support everything from one dimensional to N-dimensional arrays

You can declare:

public object this[int i] {...} // 1 dimensional

public object this[int i1, int i2] {...} // 2 dimensional

public object this[params int[] indexes] {...} // N dimensional

Just like GetValue()/SetValue() are overloaded.





Re: .NET Base Class Library Why doesn't System.Array have an indexer?

nobugz

Note how GetValue() peters out beyond 3 dimensions and gets very inefficient after that, requiring passing passing a array of indexes. I can think of only a few reasons why System.Array wasn't declared abstract. CreateInstance() for non-zero lower bounds perhaps...





Re: .NET Base Class Library Why doesn't System.Array have an indexer?

Mattias Sjogren

thrace wrote:

You can declare:

public object this[int i] {...} // 1 dimensional

public object this[int i1, int i2] {...} // 2 dimensional

public object this[params int[] indexes] {...} // N dimensional

Just like GetValue()/SetValue() are overloaded.

But since each concrete array type has its own indexer, that could introduce an ambiguity in chosing the right overload (the CTS and many languages including C# doesn't allow overloading based on only the return type). For example in an int[] you'd have

public object this[int i] {...}

in the System.Array base class, and effectively

public int this[int i] {...}

in the array type itself.






Re: .NET Base Class Library Why doesn't System.Array have an indexer?

nobugz

Interesting idea. How about this:

public class Base {
private object[] values = { 1, 2, 3 };
public object this[int index] {
get { return values[index]; }
set { values[index] = value; }
}
}
public class Derived : Base {
public new int this[int index] {
get { return (int)base[index]; }
set { base[index] = value; }
}
}
public static class Test {
public static void Run() {
Derived d = new Derived();
int i = d[0];
d[0]++;
Base b = (Base)d;
object o = b[0];
}
}

It works but it is quite expensive...