C# Array CreateInstance() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Array

Definition

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);
CallWhat 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

ParameterDescription
elementTypeRequired. The type of each element, such as typeof(int).
length / lengthsRequired. Size of each dimension. Must be non-negative.

Return Value

Return ValueDescription
ArrayA 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);
    }
}

How It Works

  1. Runtime type. typeof(int) tells CreateInstance to store ints.
  2. Cast once. Casting to int[] unlocks normal nums[i] indexing.
  3. Fill loop. Each slot starts at 0; the loop writes 10, 20, 30, 40.
  4. 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.

C#
using System;

class Program {
    static void Main() {
        Array grid = Array.CreateInstance(typeof(string), 2, 3);
        string[,] cells = (string[,])grid;

        cells[0, 0] = "A";
        cells[0, 1] = "B";
        cells[1, 2] = "Z";

        Console.WriteLine("Rank: " + cells.Rank);
        Console.WriteLine("GetLength(0): " + cells.GetLength(0));
        Console.WriteLine("GetLength(1): " + cells.GetLength(1));
        Console.WriteLine("cells[1,2] = " + cells[1, 2]);
    }
}

How It Works

  1. Two lengths. Dimensions are 2 rows and 3 columns.
  2. Rank. Rank is 2 because the array has two dimensions.
  3. Per-dimension size. Use GetLength(dimension), not a single Length for each side.
  4. Unset cells. Other string slots remain null until assigned.

Example 3 — Create From a Type Variable

When the element type arrives as a Type, CreateInstance is the safe factory.

C#
using System;

class Program {
    static Array MakeZeros(Type t, int n) {
        return Array.CreateInstance(t, n);
    }

    static void Main() {
        Array doubles = MakeZeros(typeof(double), 3);
        doubles.SetValue(1.5, 0);
        doubles.SetValue(2.5, 1);

        Console.WriteLine(doubles.GetValue(0));
        Console.WriteLine(doubles.GetValue(1));
        Console.WriteLine(doubles.GetValue(2));
        Console.WriteLine(doubles.GetType().Name);
    }
}

How It Works

  1. Factory helper. MakeZeros does not hard-code double[] in its signature.
  2. SetValue / GetValue. Work on the non-generic Array without casting.
  3. Default element. Index 2 was never set, so it prints the default 0.
  4. Runtime type name. GetType().Name shows Double[].

Frequently Asked Questions

It creates a new array whose element type and lengths you pass at runtime. The result is typed as System.Array until you cast it.
Use CreateInstance when the element Type is not known at compile time, or when you need special lower bounds. Otherwise prefer new T[n].
Yes. Pass multiple lengths: Array.CreateInstance(typeof(int), 2, 3) creates a 2×3 int array.
System.Array. Cast to int[], string[,], or another concrete array type when you want typed indexing.
Yes. Value types become their default (0, false). Reference types become null.

Next: Empty()

Return a shared empty array of type T without allocating each time with System.Array.

Empty() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

15 people found this page helpful