添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

This browser is no longer supported.

Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.

Download Microsoft Edge More info about Internet Explorer and Microsoft Edge

Starting in C# 9, you don't have to explicitly include a Main method in a console application project. Instead, you can use the top-level statements feature to minimize the code you have to write. In this case, the compiler generates a class and Main method entry point for the application.

Here's a Program.cs file that is a complete C# program in C# 10:

Console.WriteLine("Hello World!");

Top-level statements let you write simple programs for small utilities such as Azure Functions and GitHub Actions. They also make it simpler for new C# programmers to get started learning and writing code.

The following sections explain the rules on what you can and can't do with top-level statements.

Only one top-level file

An application must have only one entry point. A project can have only one file with top-level statements. Putting top-level statements in more than one file in a project results in the following compiler error:

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

A project can have any number of additional source code files that don't have top-level statements.

No other entry points

You can write a Main method explicitly, but it can't function as an entry point. The compiler issues the following warning:

CS7022 The entry point of the program is global code; ignoring 'Main()' entry point.

In a project with top-level statements, you can't use the -main compiler option to select the entry point, even if the project has one or more Main methods.

using directives

If you include using directives, they must come first in the file, as in this example:

using System.Text; StringBuilder builder = new(); builder.AppendLine("Hello"); builder.AppendLine("World!"); Console.WriteLine(builder.ToString());

Global namespace

Top-level statements are implicitly in the global namespace.

Namespaces and type definitions

A file with top-level statements can also contain namespaces and type definitions, but they must come after the top-level statements. For example:

MyClass.TestMethod(); MyNamespace.MyClass.MyMethod(); public class MyClass public static void TestMethod() Console.WriteLine("Hello World!"); namespace MyNamespace class MyClass public static void MyMethod() Console.WriteLine("Hello World from MyNamespace.MyClass.MyMethod!");

Top-level statements can reference the args variable to access any command-line arguments that were entered. The args variable is never null but its Length is zero if no command-line arguments were provided. For example:

if (args.Length > 0) foreach (var arg in args) Console.WriteLine($"Argument={arg}"); Console.WriteLine("No arguments");

await

You can call an async method by using await. For example:

Console.Write("Hello "); await Task.Delay(5000); Console.WriteLine("World!");

Exit code for the process

To return an int value when the application ends, use the return statement as you would in a Main method that returns an int. For example:

string? s = Console.ReadLine(); int returnValue = int.Parse(s ?? "-1"); return returnValue;

Implicit entry point method

The compiler generates a method to serve as the program entry point for a project with top-level statements. The name of this method isn't actually Main, it's an implementation detail that your code can't reference directly. The signature of the method depends on whether the top-level statements contain the await keyword or the return statement. The following table shows what the method signature would look like, using the method name Main in the table for convenience.

Top-level code contains Implicit Main signature

C# language specification

For more information, see the C# Language Specification. The language specification is the definitive source for C# syntax and usage.

Feature specification - Top-level statements