Skip to main content

How to Resolve Error "CS0622: Can only use array initializer expressions to assign to array types" in C#

The Compiler Error CS0622 is a syntax error regarding Array Initializers. The message reads: "Can only use array initializer expressions to assign to array types. Try using a new expression instead."

In C#, the shorthand syntax { value1, value2 } is strictly reserved for initializing arrays at the moment of their declaration. If you try to use this shorthand to initialize a non-array type (like a simple int, a List<T>, or an object), or if you try to pass it directly as a method argument without the new keyword, the compiler rejects it.

This guide explains the limitations of the array initializer shorthand and how to use the explicit new syntax to fix the error.

Understanding Array Initializer Syntax

C# allows a convenient shorthand for creating arrays:

// Valid: The type 'int[]' is declared on the left.
// The compiler knows '{ 1, 2, 3 }' creates an int[].
int[] numbers = { 1, 2, 3 };

However, this shorthand has a limitation: The compiler must be able to infer that the target is specifically an Array Type. If the target is an object, a List, or a scalar variable, this shorthand is invalid.

Scenario 1: Initializing Non-Array Variables

This error commonly occurs when developers try to use the curly brace syntax to initialize objects or primitive types that are not arrays.

Example of error (Scalar Types)

Trying to assign a "set" of values to a single integer.

public void Process()
{
// ⛔️ Error CS0622: Can only use array initializer expressions to assign to array types.
// 'x' is an int, not an array.
int x = { 10 };
}

Example of error (Object or Dynamic)

Trying to assign an array literal to a loosely typed variable.

public void Process()
{
// ⛔️ Error CS0622: 'obj' is not explicitly an array type.
// The shorthand syntax is forbidden here.
object obj = { 1, 2, 3 };
}

Scenario 2: Passing Initializers as Arguments

You cannot pass the shorthand syntax {...} directly to a method call, even if that method expects an array. The compiler does not "look ahead" into the method signature to apply the shorthand transformation.

Example of error

public void PrintNumbers(int[] numbers)
{
// ...
}

public void Run()
{
// ⛔️ Error CS0622: You cannot use the shorthand syntax as an argument.
// The compiler sees a block of code '{ 1, 2 }', not an array creation.
PrintNumbers({ 1, 2, 3 });
}

Solution: Use the new Expression

To fix this error, you must be explicit. Use the new keyword followed by the specific type you want to create (e.g., new int[], new List<int>).

Fixing Variable Assignment

If you really meant to create an array but store it in an object variable, be explicit.

public void Process()
{
// ✅ Correct: Explicitly creating an array
object obj = new int[] { 1, 2, 3 };
}

Fixing Method Arguments

When calling a method, you must fully declare the array.

public void Run()
{
// ✅ Correct: 'new int[]' tells the compiler exactly what to create.
PrintNumbers(new int[] { 1, 2, 3 });
}
note

Collection Expressions (C# 12+): In C# 12 and newer (.NET 8+), a new syntax [ 1, 2, 3 ] (square brackets) was introduced. This Collection Expression syntax is allowed in many places where the old curly brace syntax { 1, 2, 3 } failed, provided the target type is compatible (like List<T>, Span<T>, or arrays).

Conclusion

CS0622 is a syntax rule regarding the { ... } shorthand.

  1. Check the Target: Are you assigning {...} to a variable that is NOT an array (like object or int)?
  2. Check Context: Are you passing {...} as a function argument?
  3. The Fix: Always use new Type[] { ... } (e.g., new int[] { 1, 2 }) to resolve ambiguity.