4

When I do int[], string[], T[] - this is a generic array. An array is just an object like everything else.

So what is the actual open generic type of []? I assume it is just some syntactic sugar over something like Array<> but I haven't been able to find anything of the sort.

Bonus points if you can somehow answer this before Jon Skeet.

abatishchev
  • 98,240
  • 88
  • 296
  • 433
George Mauer
  • 117,483
  • 131
  • 382
  • 612
  • 5
    Remember, arrays were designed *before* generics were added to the framework. If we were doing it again from scratch and generics were in v1 then sure, arrays would probably be Array, just like nullables are Nullable. You are right that arrays are very "generic like"; in a sense arrays were the *only* generic type in the v1 CLR, which is why they are such a special type. Next time you design a type system of your own from scratch, remember to add generics early, rather than adding them on later. – Eric Lippert Mar 04 '11 at 22:51
  • Thanks @Eric, I read your article linked to by Mark, very interesting. I guess I had assumed that when 2.0 came around the internals of Array got redesigned. Given the limitations, would it not be a good idea to create a generic IArray version? Any reason why that did not happen? After all, an array is certainly different from IList and IEnumerable – George Mauer Mar 05 '11 at 04:37
  • 1
    I don't know exactly what the CLR team was thinking during that release but I can make an educated guess. My thought is that "the perfect is the enemy of the good". Given that there already was IList and there was going to be IList and IEnumerable it seems reasonable to just say "well, arrays are close enough to IList to make it work, so let's just do that rather than introduce a *third* concept IArray". Again, if generics had been there first then it probably would have gone down differently. – Eric Lippert Mar 05 '11 at 07:00
  • I just found Array.CreateInstance(type, length) ... – Robert Cutajar Sep 24 '13 at 11:57

4 Answers4

8

It's just System.Array; it isn't generic. See here for a brief discussion: http://blogs.msdn.com/b/ericlippert/archive/2007/10/17/covariance-and-contravariance-in-c-part-two-array-covariance.aspx

3dGrabber
  • 4,710
  • 1
  • 34
  • 42
Mark Sowul
  • 10,244
  • 1
  • 45
  • 51
  • Its not generic? But its directly castable to IEnumerable – George Mauer Mar 04 '11 at 22:27
  • 7
    Because System.Array implements `IEnumerable`. And the generic ICloneable, IList, ICollection, IEnumerable, IStructuralComparable, IStructuralEquatable, etc. However, the framework provides these implementations at runtime because Array itself isn't generic. – KeithS Mar 04 '11 at 22:31
4

Arrays are older that generics. Arrays are available since .net 1, and generics were only introduced in .net 2. So they are no syntax sugar on top of generics.

Instead they derive from Array and use a lot of runtime magic to work.

In addition arrays of a reference type support some variance patterns not supported by generics. If I recall correctly this was done for Java compatibility.

CodesInChaos
  • 106,488
  • 23
  • 218
  • 262
3

Far as I know, there wouldn't be an open generic type of Array; they existed before generics did in the .NET Framework. Arrays are just "weird"; the System.Array type exists mainly to provide O-O functionality to array objects. Only the runtime can derive from it.

HOWEVER, System.Array implements the generic IList<T>, which enforces the indexer. If you want an abstract that is strongly typed and will accept a strongly-typed array, use IList.

KeithS
  • 70,210
  • 21
  • 112
  • 164
1

It's not just syntactic sugar, it's an actual derivation of the System.Array class.