Search code examples
c#.netvisual-studio-codeentry-pointtoplevel-statement

C# Only one compilation unit can have top-level statements


I just started learning c#, I created C# console application. To understand the concepts, I watched videos of how to setup vs code for c#

When I run the dotnet new console command in VS code terminal, it creates a new project including Program.cs file.

In the video, the Program.cs file appears like that

// Program.cs
using System;
namespace HelloWorld
{
  class Program
  {
    static string Main(string[] args)
    {
      Console.WriteLine("Hello, World!");
    }
  }
}

Program.cs in my IDE appears like,

// Program.cs
// See https://aka.ms/new-console-template for more information
Console.WriteLine("Hello, World!");

When I run the code using terminal dotnet run it runs perfectly on my computer.

when I create a new cs file, it contains

// hello.cs
Console.WriteLine("hello world");

after running it says Only one compilation unit can have top-level statements.

when I use class method and namespace like

// hello.cs
namespace helloworld
{
    class hello
    {
        static void Main()
        {
            Console.WriteLine("hello world");

        }
    }
}

it runs THE Program.cs file not the new file and shows this warning

PS C:\Users\User\C#projects> dotnet run hello.cs C:\Users\User\C#projects\hello.cs(5,21): warning CS7022: The entry point of the program is global code; ignoring 'hello.Main()' entry point. [C:\Users\User\C#projects\C#projects.csproj] Hello, World!

Project structure:

enter image description here

I tried another method by pressing run and debug and show nothing.

When I click on Generate c# Assets for Build and Debug button it shows this

Could not locate .NET Core project. Assets were not generated.


Solution

  • C# 9 feature: Top-level statements

    This is a newly introduced feature in C# 9 called Top-level statements

    The video to which you are referring might be using a lower version of C#(lower than C# 9). Where we use to get

    namespace helloworld
    {
        class hello
        {
            static void Main()
            {
                Console.WriteLine("hello world");  
            }
        }
    }
    

    as a default structure of Main program.

    If you look closely you will find, only one line of code which prints a string to the console i.e.

    Console.WriteLine("hello world");  
    

    Top-level statements were introduced to remove unnecessary ceremony from this console application.

    As you are using C#9 or higher, dot net run with top-level statement compiling your code successfully, but when you are replacing one-liner code to legacy structure, then compiler warns you regarding the global entry of Main function and Main() function which you added by replacing top-level statement.

    To Get more clarity you can go through MSDN documentation: Top-level statements


    Why you are getting an error "Only one compilation unit can have top-level statements."?

    • As per the MSDN documentation, An application must have only one entry point.
    • A project can have only one file with top-level statements.
    • When you created a new file, you added a new top-level statement which result in below compile time error:

    CS8802 Only one compilation unit can have top-level statements.


    how to fix it?

    • As per the above explanation, your project should not contain two or more top-level statements. To fix this error, you can delete a file that was added later on.