Skip to content

How to Resolve Error "CS0623: Array initializers can only be used in a variable or field initializer" in C#

Learn how to fix the C# compiler error CS0623 by using the explicit new syntax or modern Collection Expressions (square brackets) for array initialization outside declarations.

The Compiler Error CS0623 is a syntax restriction error involving Array Initializers. The message reads: "Array initializers can only be used in a variable or field initializer. Try using a new expression instead."

In C#, the curly brace shorthand { value1, value2 } is strictly limited to the exact moment you declare a variable or field. You cannot use this shorthand to assign a value to an existing variable, nor can you use it in a return statement or passing arguments. If you attempt to use { ... } anywhere other than the definition line, the compiler raises CS0623.

This guide explains the limitations of this shorthand and how to use the explicit new syntax or modern Collection Expressions to fix it.

Understanding the Shorthand Restriction

The syntax { 1, 2, 3 } is "syntactic sugar". The compiler only understands how to translate this into an array creation instruction when it sees a type declaration immediately to the left.

  • Valid: int[] numbers = { 1, 2, 3 }; (Declaration and Assignment together).
  • Invalid: numbers = { 1, 2, 3 }; (Assignment only).

When doing assignment only, the compiler doesn't infer the type for the {} block, so it requires you to be explicit.

Scenario 1: Reassigning an Existing Variable

This is the most common cause. You declare an array, and later in the code, you try to reset it using the shorthand syntax.

Example of error:

public void Process()
{
    int[] numbers;

    // ... some logic ...

    // ⛔️ Error CS0623: Array initializers can only be used in a variable or field initializer.
    // The variable 'numbers' is already declared. You cannot use '{...}' here.
    numbers = { 1, 2, 3 }; 
}

Scenario 2: Returning an Array

You cannot return the curly brace shorthand directly from a method, even if the return type is an array.

Example of error:

public int[] GetPrimes()
{
    // ⛔️ Error CS0623: Try using a new expression instead.
    return { 2, 3, 5, 7 };
}

Solution 1: Use the new Keyword (Standard Fix)

To make the assignment valid anywhere in your code, you must explicitly use the new operator followed by the array type. This tells the compiler exactly what kind of object to create.

Fixing Reassignment

public void Process()
{
    int[] numbers;

    // ✅ Correct: Explicitly creating a new integer array
    numbers = new int[] { 1, 2, 3 };
}

Fixing Return Statements

public int[] GetPrimes()
{
    // ✅ Correct: Explicit creation
    return new int[] { 2, 3, 5, 7 };
}

Solution 2: Use Collection Expressions (C# 12+)

If you are using .NET 8 (C# 12) or newer, you can replace the curly braces {} with square brackets []. This new syntax, called Collection Expressions, is valid in assignments, return statements, and arguments.

The Modern Fix

public class ModernExample
{
    public int[] numbers;

    public void Update()
    {
        // ✅ Correct (C# 12+): Square brackets work for assignment
        numbers = [1, 2, 3];
    }

    public int[] GetValues()
    {
        // ✅ Correct (C# 12+): Works for return values too
        return [10, 20, 30];
    }
}

Conclusion

CS0623 is a syntax rule preventing ambiguity in older C# versions.

  1. Check Context: Are you using { ... } after the variable has already been declared?
  2. Standard Fix: Add new Type[] before the curly braces (e.g., numbers = new int[] { 1, 2 };).
  3. Modern Fix: If on C# 12+, switch to square brackets [ ... ] to enable cleaner syntax everywhere.
Navigation