Home > Software engineering >  C# vs code bug `Only one compilation unit can have top-level statements` (beginner)
C# vs code bug `Only one compilation unit can have top-level statements` (beginner)

Time:06-07

i just want to learn c# i created c#projects folder
the issue am facing is when i watch videos of how to setup vs code for c#
and when i run the code dotnet new console in vscode terminal it creates new stuffs in my project includes Program.cs file and there the issue begin
in the videos the Program.cs file appears like that

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

when my Program.cs in my ide appears like

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

and when i run the code using terminal dotnet run it runs perfectly on my pc like the video BUT when i create new cs file 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!
and here how's the project looks like : screenshot of project
i tried other 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.

CodePudding user response:

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

Video which you are referring might be using 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 string to the console i.e.

Console.WriteLine("hello world");  

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

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 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

CodePudding user response:

In C# 9 You can use top level statement in only one file please refer to the docs

https://docs.microsoft.com/en-us/dotnet/csharp/fundamentals/program-structure/top-level-statements#only-one-top-level-file

  • Related