[Continuously updating] C# common problems and their solutions (VS2019)

VS2019 Error: CS8370 Feature 'top-level statement' is not available in C# 7.3. Please use language version 9.0 or higher.

Error :
insert image description here
Cause : The Top-level statement is a feature added in C# 9. Prior to C# 9, the entry point was a static method Mainnamed .

Solution :
In order to enable C# 9.0 in the project, you need to edit the .csproj file and add the following:

<PropertyGroup>
   <LangVersion>9.0</LangVersion>
</PropertyGroup>

Alternatively, the following configuration can be used to target the latest C# version (currently 11.0):

<PropertyGroup>
   <LangVersion>latest</LangVersion>
</PropertyGroup>

Open the .csproj file (xml format) under the project file directory , insert image description here
find <PropertyGroup>under the node <LangVersion>, and change its value to 9.0.
If there is no such byte point, add the node directly after the node <PropertyGroup>under the node : after reloading the project, no error is reported, and the problem is solved:<WarningLevel><LangVersion>
insert image description here

insert image description here

The reason why the console program flashes

Problem : Click the "Start" button in the menu bar or directly press the "F5" key on the keyboard, and find that the program is running, but the console is automatically closed before seeing the content displayed on the console.

Reason : F5 is the Debugging mode . In this mode, the window will not remain open after the program finishes running. And Ctrl+F5 is the Start Without Debugging mode , in this mode, you can see the running results.

Solution : Press "Ctrl+F5" to run.

The entry point of the CS7022 program is global code; the "TestClass.Main(string[])" entry point is ignored.

Problem: If you use the top-level program function, you no longer use Main as the entry point of the program. If you write it, you will not report an error, and there will be a warning, which will be automatically ignored when the program is running.

Solution: Delete the Main entry point.

Guess you like

Origin blog.csdn.net/qq_41084756/article/details/124244566