There is no Main after creating a console application in VS2022. How to display the Main?

Problem Description

After creating a console application with VS2022, there is no namespace and Main function, only one WriteLine, as shown below.

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

reason

First, let me explain the reason. In VS2022, the main framework used to create console applications is .NET6.0, and this console with only one WriteLine statement is the new template of .NET6.0, which uses the top-level statement function.
Only .Net5.0 and below versions will generate the previous template with Main and namespace.
This article does not cover the specific explanation of top-level statements. If you are interested, you can study it on your own.
This article mainly explains how to create previous templates.

solution

To create a previous template, you only need to check "Don't use top-level statements" on the framework selection page when creating a console application.
Insert image description here

Simple top-level statement trial

Top-level statements in C# are statements that can be written directly in the source file without being included inside any class or method. They were introduced primarily to simplify the writing and debugging of small programs.

In previous C# versions, we had to put our code in a structure of classes and methods. To define an executable program, we needed to write a method called Main in a class and write our code in it. However, such a structure may be too cumbersome for some small programs and increase the complexity of the program.

Starting from C# 9.0, we can use top-level statements to write more concise programs. Top-level statements are usually located at the outermost level of the source file, and we do not need to explicitly define classes and methods and can directly write our code.

The use of top-level statements is very simple. You can use the using statement in the top-level statement to introduce namespaces, define variables and constants, call methods, perform calculations, and so on. The compiler will automatically convert these top-level statements into the structure of classes and methods, and organize the code according to certain rules.

Note that only one compilation unit can have top-level statements. This is to keep the code readable and consistent. If we want to write more complex programs, we still need to use the structure of classes and methods to organize the code.
Insert image description here
The introduction of top-level statements makes writing small programs more concise and intuitive. No longer needing to explicitly define classes and methods, we can express our intentions more directly. This is useful for prototyping, small scripts, and quick testing.

calculator

Next, I will explain how to use top-level statements to write a simple calculator program so that everyone can get better accustomed to top-level statements. The calculator lets the user enter two numbers and calculates their sum, as shown below:
Insert image description here

Console.WriteLine("没错,是计算器!");

Console.Write("请输入第一个数字:");
#pragma warning disable CS8604 // 引用类型参数可能为 null。
int num1 = int.Parse(Console.ReadLine());

Console.Write("请输入第二个数字:");
int num2 = int.Parse(Console.ReadLine());
#pragma warning restore CS8604 // 引用类型参数可能为 null。

int sum = num1 + num2;
Console.WriteLine($"数字 {
      
      num1}{
      
      num2} 的和为 {
      
      sum}。");

Console.WriteLine("结束!");

The above code uses Console.WriteLine at the top level to output a welcome message. Next, the program prompts the user for two numbers and uses int.Parse to convert the entered string into an integer value.

The program then calculates the sum of the two numbers and prints the result using string interpolation syntax.

Other articles

How to get the LAN IP of the current host in C#
C# implements sending messages between LANs

Guess you like

Origin blog.csdn.net/weixin_44499065/article/details/133253014