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

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

C# Compiler Error CS0442 – ‘Property’: abstract properties cannot have private accessors Reason for the Error You’ll get this error...
This is a really simple one . Below is a simple example of an enum called “Designation” defined with the...
This blog post explain the usage of the Checked Block in .NET and how you can use them in Visual...