Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't Array class expose its indexer directly?

Tags:

something to mention for answering:

  1. Don't worry about variance, while the item in question is Array rather than T[].

  2. A similar case for multi-dimension arrays is [here]

That is, N-dims to linear transform, is always possible. So this question especially caught my attention, since it already implemented IList for a linear indexer.


Question:

In my code, I have following declaration:

public static Array ToArray<T>(this T source);  

My code knows how to make souce presents an array(at runtime). And I'm trying to allow the consuming code to access its indexer directly. But without "as IList", it cannot not be done. To return object[] might require extra converting/casting, that's what I'm preventing to do. What I can do is:

public static IList ToArray<T>(this T source);  

But I would think that a method named ToArray returns an IList looked strange.

Thus, I'm confused with that:

In the declaration of Array, there is

object IList.this[int index]; 

So that we can

Array a; a=Array.CreateInstance(typeof(char), 1); (a as IList)[0]='a'; 

But we cannot

a[0]='a'; 

except if it was declared as

public object this[int index];  

The only difference I can see is that it requires we use its indexer explicitly through the interface IList by which it was implemented, but why? Are there benefits? Or are there exposing issues?

like image 801
Ken Kin Avatar asked Jan 26 '13 02:01

Ken Kin


People also ask

What is difference between indexers and array?

Array:- An array is a collection of variables of the same type that are referred to by a common name. Indexers:- An indexer allows an object to be indexed like an array. The main use of indexers is to support the creation of specialized arrays that are subject to one or more constraints.

Does an indexer allows an object to be indexed?

Indexers enable objects to be indexed in a similar manner to arrays. A get accessor returns a value. A set accessor assigns a value. The this keyword is used to define the indexer.

What can be accessed using indexers?

Indexers are a syntactic convenience that enable you to create a class, struct, or interface that client applications can access as an array. The compiler will generate an Item property (or an alternatively named property if IndexerNameAttribute is present), and the appropriate accessor methods.

Can indexer have get and set block?

Indexers are implemented through get and set accessors for the [ ] operator. ref and out parameter modifiers are not permitted in indexer. The formal parameter list of an indexer corresponds to that of a method and at least one parameter should be specified.


1 Answers

Array can't have an indexer because it needs to be able to represent an array with any number of dimensions. The indexer for a two dimensional array has a different signature than for a one dimensional array.

If an indexer was provided and used on an Array that represented a two dimensional array what should happen?

The solution that the language designers choose was to just not include an indexer at all.

If you know that your ToArray method will always return a one dimensional array then consider using:

public static T[] ToArray<T>(this T source);  

That will have an indexer.

If the elements in the array will not all be of type T then you can return an object[]:

public static object[] ToArray<T>(this T source);  
like image 113
Servy Avatar answered Oct 06 '22 05:10

Servy