error CS5001: Program does not contain a static 'Main' method suitable for an entry point

Vasya Pupkin 60 Reputation points
2024-09-01T01:07:27.0366667+00:00

Hey there,

So... I downloaded .NET 8 and opened cmd to create a default console template using the following command:

dotnet new console --output "D:\User Files\A_L_L\DOWNLOAD_TEMP\test"

It generated a project. The cs file (Program.cs) inside the project has these contents:

// See https://aka.ms/new-console-template for more information

Console.WriteLine("Hello, World!");

Now, when I try to build the project using the following command, it generates the error (described in the title):

dotnet build "D:\User Files\A_L_L\DOWNLOAD_TEMP\test\test.csproj" --artifacts-path "D:\User Files\A_L_L\DOWNLOAD_TEMP\test"

I visited the link found in the cs file (https://aka.ms/new-console-template) in hopes to find some info. This is what it says:

When you use the newer version, you only need to write the body of the Main method. The compiler generates a Program class with an entry point method and places all your top level statements in that method. The name of the generated method isn't Main, it's an implementation detail that your code can't reference directly. You don't need to include the other program elements, the compiler generates them for you.

I read it that it was supposed to work. But what is the problem?

Thanks

EDIT: By the way, what if I want to write another method/function outside the entry point and reference it inside the entry point? How does the code inside the Program.cs know where to look for the outside function (that I wrote)?

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,841 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,914 questions
0 comments No comments
{count} votes

Accepted answer
  1. P a u l 10,731 Reputation points
    2024-09-01T12:44:12.89+00:00

    This is probably because you're setting the artifacts-path to be the project folder (.\test). You could set it to .\test\artifacts or another nested folder, but typically you'd use this flag for configuring a common artifact folder location for all projects in a solution to simplify CI/CD pipelines. So normally you'd set it to a folder directly inside your solution folder, or somewhere else entirely.

    https://learn.microsoft.com/en-us/dotnet/core/sdk/artifacts-output

    All build outputs from all projects are gathered into a common location, separated by project. A common location makes it easy for tooling to anticipate where to find the outputs.
    

    For a simple hello world console app you can just omit that flag all together, though.

    As for how to define a method/function: if there's code that you want the compiler to wrap a fake Main method around then it goes at the top, so your declarations can go at the bottom, but in reality it's probably best to avoid Program.cs being too complex & just moving these functions out.

    Print();
    
    static void Print() {
    	Console.Write("Hello, World!");
    }
    
    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.