Expand To Show Full Article
C# Error CS0820 - Cannot initialize an implicitly-typed variable with an array initializer - Developer Publish

C# Error CS0820 – Cannot initialize an implicitly-typed variable with an array initializer

C# Error

CS0820 – Cannot initialize an implicitly-typed variable with an array initializer

Reason for the Error & Solution

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;  
    }  
}