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

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

This C# program calculates and displays an upper triangular matrix based on user input. Problem Statement: The program takes the...
This C# program serves as a demonstration of bitwise operators, which are fundamental operators used for manipulating individual bits in...
This C# program is designed to interchange or swap the columns of a matrix. A matrix is a two-dimensional array...