Create a new array without language syntax []

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

I tried new System.Array<int>(3), but it exploded

The custom type 'System.Array' cannot be used with type arguments

+5
source share
3 answers

System.Array , . , , . (. ):

!

.NET Framework 2.0 Array System.Collections.Generic.IList<T>, System.Collections.Generic.ICollection<T> System.Collections.Generic.IEnumerable<T>. .

System.Array CreateInstance(), :

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

Array CreateInstance , .

, Array ! , , ( , int[] array ):

var array = (int[]) Array.CreateInstance(typeof(int), 3);
+9

"" . - new type[length] CreateInstance Array. , Array, .

BTW: Array.CreateInstance , #. , , IList , .

+1

You can also use the following in C # if you want to assign values ​​when creating an array instance.

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

-2
source

All Articles