4

In C# one can instantiate an Array with the special square bracket syntax new int[3]. This is different to other types which you instantiate by calling the constructor new List<int>(). Can you use the ordinary syntax to create an Array?

I tried new System.Array<int>(3) but it blows up

The non-generic type 'System.Array' cannot be used with type arguments

bland
  • 1,968
  • 1
  • 15
  • 22
Colonel Panic
  • 132,665
  • 89
  • 401
  • 465
  • The error message is telling you that you can't do that. – Hunter McMillen May 31 '12 at 16:43
  • 1
    No, you can't do that. Why do you want to? – Tim S. May 31 '12 at 16:43
  • 1
    Not sure what you're after, but a "different" (but worse) way of doing it: `var newIntArray = (int[]) Array.CreateInstance(typeof (int), 10);` – vcsjones May 31 '12 at 16:43
  • you can try setting / creating a dynamic array.. when you declare the array declare it like this for example string[] myString = {}; then you can set the size based on the size of the List for example – MethodMan May 31 '12 at 16:43

3 Answers3

8

System.Array itself is abstract, so you won't be able to instantiate it using its constructor. And as evidenced by your error, it's not actually generic either; it only gets its type through implementing generic collection interfaces at runtime (see also this related answer):

Important

In the .NET Framework version 2.0, the Array class implements the System.Collections.Generic.IList<T>, System.Collections.Generic.ICollection<T>, and System.Collections.Generic.IEnumerable<T> generic interfaces. The implementations are provided to arrays at run time, and therefore are not visible to the documentation build tools.

For the above reasons, the System.Array class provides a convenience method called CreateInstance() which you can use instead:

var array = Array.CreateInstance(typeof(int), 3);

Remarks

Unlike most classes, Array provides the CreateInstance method, instead of public constructors, to allow for late bound access.

Be very careful when using this method, though, as the resultant array will not be strongly-typed at compile-time! If you want it to be strongly-typed you must cast it after creating it (and no, simply declaring int[] array won't be enough):

var array = (int[]) Array.CreateInstance(typeof(int), 3);
Community
  • 1
  • 1
BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
  • don't you need to assign that to something? – Hunter McMillen May 31 '12 at 16:45
  • @Hunter McMillen: Psh. Added it in now. – BoltClock May 31 '12 at 16:47
  • 1
    there is another way of doing this as well by creating dynamic Array, then if the OP decides to use List to hold the information the OP could assign the contents of the List.ToArray() based on what he has declared dynamically {} I could post a working example if you would like to see.. Good Answer by the way BoltClock – MethodMan May 31 '12 at 17:02
1

There's no "ordinary" constructor syntax to create an array. Your only choices are the array constructor syntax new type[length] and the various static CreateInstance methods of the Array class. Note that the latter are defined with the return type Array, which needs to be casted to the actual array type first.

BTW: Array.CreateInstance also allows you to create non-zero based arrays, which are otherwise unavailable to C#. Keep in mind, though, that almost nobody expects arrays and other IList implementations to have a lower bound other than zero.

Wormbo
  • 4,978
  • 2
  • 21
  • 41
-2

You can also use the following in c# if you want to assign values when instantiating the array.

predefinedIntArrayVariable = new int[]{x,y,z};

  • 1
    The OP is specifically asking if you can create an array *without* using that syntax. – Servy Oct 08 '13 at 21:13