[Solved] What does “Primary constructor body not allowed” mean?

I am attempting to compile and run this C# program in Terminal:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

public class largestNumber
    public static void Main(string[] args);
        int UserNumber1 = int.Parse(Console.ReadLine("Enter a number:"));
        int UserNumber2 = int.Parse(Console.ReadLine("Enter a second number:"));
        int UserNumber3 = int.Parse(Console.ReadLine("Enter a third number:"));

        Console.WriteLine("Your numbers were, " + UserNumber1 + ", " + UserNumber2 + ", and " + UserNumber3);

however, when I do so, this error comes up:

Number.cs(11,2): error CS9010: Primary constructor body is not allowed

Can someone explain what the error is referring to?

Solution #1:

Multiple problems with your app:

  • Method implementations do not take a ; at the end.

  • Console.ReadLine does not take any arguments. Its only purpose is to read the console, so you should use Console.WriteLine to ask your questions like “Enter a number”.

  • You should wrap your class in a namespace (Which is usually named like your .csproj project)

And please follow C# conventions, with class names as PascalCase and variables as camelCase. When you share your code, it’s easier for other developers to read and understand it.

So it should look like this:

using System;

namespace MyApp
    public class LargestNumber
        public static void Main(string[] args)
            Console.WriteLine("Enter a number:");
            int userNumber1 = int.Parse(Console.ReadLine());
            Console.WriteLine("Enter a second number:");
            int userNumber2 = int.Parse(Console.ReadLine());
            Console.WriteLine("Enter a third number:");
            int userNumber3 = int.Parse(Console.ReadLine());

            Console.WriteLine("Your numbers were, " + userNumber1 + ", " + userNumber2 + ", and " + userNumber3);

Please note I removed some of your useless using statements.

You should also rename your file Number.cs to LargestNumber.cs, you usually want your file to have the same name as your class.

Also, I’m not getting the same compilation error as you, even by copy-pasting your code.

Under VS2015 I have “Non-abstract and non-extern method must declare a body” (caused by the extra semi-colon) and “Method ReadLine has 0 parameter(s) but is invoked with 1 argument(s)”. The error you’re getting may be caused by syntax errors somewhere else in your code.

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Leave a Reply

Your email address will not be published.