C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Tip: We could pass a variable reference of type "Type" instead of the typeof() operator result.
TypeTypeof, nameofSo: To create a one-dimensional array, only pass one integer after the type. To create a two-dimensional array, pass 2 integers.
C# program that uses Array.CreateInstance method
using System;
class Program
{
static void Main()
{
// [1] Create a one-dimensional array of integers.
{
Array array = Array.CreateInstance(typeof(int), 10);
int[] values = (int[])array;
Console.WriteLine(values.Length);
}
// [2] Create a two-dimensional array of bools.
{
Array array = Array.CreateInstance(typeof(bool), 10, 2);
bool[,] values = (bool[,])array;
values[0, 0] = true;
Console.WriteLine(values.GetLength(0));
Console.WriteLine(values.GetLength(1));
}
}
}
Output
10
10
2
And: This Type does not need to be statically determined (before execution) by the C# compiler.
Note: With newer versions of the C# language, generic types have alleviated this requirement.