Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Error CS0560: Bad array declarator; Error CS0270: Array size cannot be specified in a variable declaration

When I try to run some code, I get the following errors:

/home/runner/Relational-Operators/Operators/Logical.cs(23,42): error CS0650: Bad array declarator: To declare a managed array the rank specifier precedes the variable's identifier. To declare a fixed size buffer field, use the fixed keyword before the field type. [/home/runner/Relational-Operators/main.csproj]
/home/runner/Relational-Operators/Operators/Logical.cs(23,43): error CS0270: Array size cannot be specified in a variable declaration (try initializing with a 'new' expression) [/home/runner/Relational-Operators/main.csproj]

This is the code block I am trying to run.

using System;

namespace Operators
{
    class Logical
    {
        public static void Run()
        {
            Console.WriteLine("Enter two binary digits > ");
            int[] ints = new int[2]{Convert.ToInt32(Console.Read()), Convert.ToInt32(Console.Read())};
            bool[] bools = new bool[2];
            for (int i = 0; i < 2; i++)
            {
                switch (ints[i])
                {
                    case 0: bools[i] = false; break;
                    case 1: bools[i] = true;  break;
                    default:
                        throw new InvalidOperationException("Integer value is not valid");
                }
            }
            bool b1, b2 = bools[0], bools[1];
            if (b1 && b2)
            {
                Console.WriteLine("true");
            }
            if (b1 || b2)
            {
                Console.WriteLine("true");
            }
            else
            {
                Console.WriteLine("false");
            }
            if (!(b1 && b2))
            {
                Console.WriteLine("true");
            }
        }
    }
}

I am at a loss. I cannot find a way to fix the errors. From everything I’ve read anywhere so far, this code should be valid for C#.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

What is causing these errors and how do I fix them?

>Solution :

The way b1 and b2 are declared and assigned values in a single statement is causing this error.

You can change it so you assign the value to each variable directly like so:

bool b1 = bools[0], b2 = bools[1];

Or even unpack them as a tuple like so:

var (b1, b2) = (bools[0], bools[1]);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading