C#如何自定义命名Main()方法?

12

快速问题,是否有一种方法可以任意命名主方法?或者它必须被称为“Main()”?


这对我来说从未出现过,因为你只能在Main()中有一行代码,然后从那里调用任何你想要的东西。不过我对答案很感兴趣。 - George Mauer
除了好奇心之外还有其他原因吗?有人引用过这样一句话:“如果调试比编写新代码难两倍,那么根据定义,你不够聪明,无法调试出你所能想出的‘最智能’的代码。” 改变约定俗成的规则也完全属于这个范畴。 - Jim L
11个回答

0

从C# 9.0开始,Main方法不再是必需的

In C# 9.0 you can just choose to write your main program at the top level instead:

using System;

Console.WriteLine("Hello World!");

Any statement is allowed. The program has to occur after the usings and before any type or namespace declarations in the file, and you can only do this in one file, just as you can have only one Main method today.

这样做可以节省一些样板代码,并且对于快速测试某些内容的控制台程序非常有用,但是对于任何更为严肃的程序,我仍然建议使用Main; 如果你正在寻找程序的入口点,很容易在整个解决方案中搜索Main(,但不容易搜索缺乏一个命名空间/类/方法。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接