In C#, Array.CreateInstance creates a new array when you know the element type and sizes at runtime. You pass a Type (for example typeof(int)) and one or more lengths.
It is handy for multi-dimensional arrays, or when the type comes from configuration. For everyday code, prefer new int[n] or collection expressions when the type is known at compile time.
This is a static member of System.Array. You call it with the class name, such as Array.CreateInstance(...). Arrays in C# have a fixed length after creation; use List<T> when the count must grow.
Syntax
Call the static method on Array. Cast the result to the typed array you need, or keep it as Array.
C#
Array raw = Array.CreateInstance(typeof(int), 4);
int[] nums = (int[])Array.CreateInstance(typeof(int), 3);
Call
What it does
Array.CreateInstance(typeof(int), 4)
Makes a one-dimensional int array of length 4 (zeros).
Array.CreateInstance(typeof(string), 2, 3)
Makes a 2×3 string array (all null).
There are overloads for lower bounds. Most beginner arrays use lower bound 0.
Library declarations
C#
public static Array CreateInstance(Type elementType, int length);
public static Array CreateInstance(Type elementType, params int[] lengths);
Parameters
Parameter
Description
elementType
Required. The type of each element, such as typeof(int).
length / lengths
Required. Size of each dimension. Must be non-negative.
Return Value
Return Value
Description
Array
A new array instance. Cast to int[], string[,], and so on when you need typed indexing.
C# Examples
Example 1 — Create a 1D Int Array
Build four ints with CreateInstance, then fill and print them.
C#
using System;
class Program {
static void Main() {
Array raw = Array.CreateInstance(typeof(int), 4);
int[] nums = (int[])raw;
for (int i = 0; i < nums.Length; i++) {
nums[i] = (i + 1) * 10;
}
Console.WriteLine(string.Join(", ", nums));
Console.WriteLine("Length: " + nums.Length);
}
}
Output
10, 20, 30, 40
Length: 4
How It Works
Runtime type.typeof(int) tells CreateInstance to store ints.
Cast once. Casting to int[] unlocks normal nums[i] indexing.
Fill loop. Each slot starts at 0; the loop writes 10, 20, 30, 40.
Length stays fixed. The array object still has length 4 after filling.
Example 2 — Create a 2×3 Matrix
Pass two lengths to make a rectangular multi-dimensional array.