Studies show that good grammar leads to more promotions and higher salaries. Write with Grammarly

C# Error CS0270 – Array size cannot be specified in a variable declaration

C# Compiler Error

CS0270 – Array size cannot be specified in a variable declaration (try initializing with a ‘new’ expression)

Reason for the Error

You’ll get this error in your C# code when you specify the size of the array in the array declaration.

For example, lets try to compile the below code snippet.

using System;

namespace DeveloperPubNamespace
{
    class Program
    {
        public static void Main()
        {
            // This will result in the error CS0270
            int[100] array1; 
            Console.WriteLine("No Error");
        }
    }
}

You’ll receive the error code CS0270 because you are using trying to specify the size of the array in its declaration.

Error CS0270 Array size cannot be specified in a variable declaration (try initializing with a ‘new’ expression) DeveloperPublish C:\Users\Senthil\source\repos\ConsoleApp3\ConsoleApp3\Program.cs 10 Active

C# Error CS0270 – Array size cannot be specified in a variable declaration

Solution

You can fix this error in your C# program by using the new operator to specify the size of the array as shown below.

using System;

namespace DeveloperPubNamespace
{
    class Program
    {
        public static void Main()
        {
            int[] array1 = new int[100];
            Console.WriteLine("No Error");
        }
    }
}