Compiler Error CS0820

Cannot assign array initializer to an implicitly typed local

An implicitly typed array is an array whose element type is inferred by the compiler. It must be initialized by using the new[] modifier as shown in the example code.

To correct this error

  • Use the new[] modifier with the array initializer.

  • Do not use an implicitly typed local variable.

Example

The following code generates CS0820 and shows how to correctly initialize an implicitly typed array:

//cs0820.cs
class G
{
    public static int Main()
    {
       
        var a = { 1,2,3}; //CS0820
        // Try using one of the following lines instead.
        // var b = new[] { 1, 2, 3 }; 
       //int[] b = {1, 2, 3};
        return -1;
    }
}

See Also

Reference

Implicitly Typed Local Variables (C# Programming Guide)