How to Resolve Error "CS0820: Cannot initialize an implicitly-typed variable with an array initializer" in C#
The Compiler Error CS0820 is a syntax error regarding Type Inference (var) combined with Array Initializers. The message reads: "Cannot initialize an implicitly-typed variable with an array initializer".
In C#, the var keyword relies on the expression on the right side of the assignment (=) to have a specific, compile-time type. An array initializer syntax like { 1, 2, 3 }, when used on its own, does not have a type; it is merely a syntactic shortcut allowed only when the target type is explicitly known. Because var doesn't know the type, and {...} doesn't provide one, the compiler reaches a stalemate.
This guide explains how to provide the missing type information so the compiler can infer the array correctly.
Understanding Array Initializers
When you write:
int[] numbers = { 1, 2, 3 };
The compiler knows you want an int[] because you declared the variable as int[]. The { 1, 2, 3 } syntax is allowed here as a shortcut.
When you write:
var numbers = { 1, 2, 3 };
The compiler looks at var and asks the right side: "What type are you?"
The right side ({ 1, 2, 3 }) answers: "I am just a list of values. I don't know if I am an array, a list, or a generic collection."
Because the expression has no type, var cannot function.
Scenario: The Naked Initializer
This error typically happens when developers want to quickly declare a list or array using var but forget the new keyword.
Example of error
public class DataSetup
{
public void LoadData()
{
// ⛔️ Error CS0820: Cannot initialize an implicitly-typed variable
// with an array initializer.
// The syntax '{ ... }' is not a valid expression by itself.
var numbers = { 1, 2, 3, 4, 5 };
// Also fails for strings
var names = { "Alice", "Bob" };
}
}
Solution 1: Use new[] (Implicitly Typed Array)
If you want to keep using var, you must use the new[] syntax. This tells the compiler: "Create an array, and figure out the array type based on the elements inside."
public class DataSetup
{
public void LoadData()
{
// ✅ Correct: 'new[]' triggers type inference for the array elements.
// The compiler sees integers, so it creates an 'int[]'.
var numbers = new[] { 1, 2, 3, 4, 5 };
// Infers 'string[]'
var names = new[] { "Alice", "Bob" };
}
}
Constraint: When using new[], all elements inside the braces must be convertible to a single common type. new[] { 1, "A" } will fail with a different error because int and string are incompatible.
Solution 2: Use Explicit Variable Typing
If you prefer the short {...} syntax, you simply cannot use var. You must explicitly declare the variable type on the left side.
public class DataSetup
{
public void LoadData()
{
// ✅ Correct: Explicit type allows the shortcut syntax.
int[] numbers = { 1, 2, 3, 4, 5 };
string[] names = { "Alice", "Bob" };
}
}
Solution 3: Use Explicit Array Creation
If you want to be explicit on both sides (often required if you are casting or using mixed types like float and int), use new Type[].
public class DataSetup
{
public void LoadData()
{
// ✅ Correct: var infers type from 'new int[]'
var numbers = new int[] { 1, 2, 3, 4, 5 };
}
}
Conclusion
CS0820 is a syntax rule regarding ambiguity.
- Understand
var: It needs an expression with a type on the right. - The Fix:
- If you want
var, addnew[]before the braces:var x = new[] { 1, 2 };. - If you want
{ ... }, removevarand use the type:int[] x = { 1, 2 };.
- If you want