快速问题,是否有一种方法可以任意命名主方法?或者它必须被称为“Main()”?
快速问题,是否有一种方法可以任意命名主方法?或者它必须被称为“Main()”?
请注意,这是C#的惯例,而不是.NET运行时的惯例。您可以在IL中任意命名方法:
.module Mane.exe
.subsystem 3
.corflags 9
.assembly extern mscorlib
{
.publickeytoken = (B7 7A 5C 56 19 34 E0 89)
.ver 2:0:0:0
}
.assembly Mane
{
.custom instance void [mscorlib]System.Runtime.InteropServices.ComVisibleAttribute::.ctor(bool) = ( 01 00 00 00 00 )
.custom instance void [mscorlib]System.CLSCompliantAttribute::.ctor(bool) = ( 01 00 01 00 00 )
.custom instance void [mscorlib]System.Reflection.AssemblyFileVersionAttribute::.ctor(string) = ( 01 00 07 31 2E 30 2E 30 2E 30 00 00 )
.custom instance void [mscorlib]System.Resources.NeutralResourcesLanguageAttribute::.ctor(string) = ( 01 00 05 65 6E 2D 55 53 00 00 )
.permissionset reqmin
= {[mscorlib]System.Security.Permissions.SecurityPermissionAttribute = {property bool 'Execution' = bool(true)}}
.hash algorithm 0x00008004
.ver 1:0:0:0
}
.namespace Mane
{
.class private abstract auto ansi sealed beforefieldinit Program extends [mscorlib]System.Object
{
.method private hidebysig static void Mane() cil managed
{
.entrypoint
.maxstack 1
ldstr "Hello, World!"
call void [mscorlib]System.Console::WriteLine(class System.String)
ret
}
}
}
Main
,否则它不会作为应用程序中的第一个方法被调用。还有一些其他要求和注意事项。来自MSDN:
我不相信在C#方面有这样的方法,但是如果你愿意编辑你的IL,很容易就可以添加.entrypoint到IL函数中。请参阅维基百科上的CLI入口。
它必须被称为Main。
如果你真的想要,你可以创建自己的方法,并将其作为代码中唯一的一行调用到 Main 中。
public static void Main(String[] args)
{
Mane(args);
}
它必须被称为Main()。
不,Main是标准的。但您可以更改托管Main方法的类,从项目属性的“启动对象”设置中更改。
但真的,为什么要这样做呢?每个人都知道它是main ... 所以您会让其他可能查看您代码的开发人员感到困惑。
你不能更改“Main”的名称,因为它是默认情况下始终会首先运行的空虚。不过,如果你想要,你可以创建另一个空虚并立即通过main调用它,虽然使用Main();可能更简单。
using System;
class Program
{
static void Main(string[] args) // Manages the login input
{
OnScriptStarted();
}
static void OnScriptStarted()
{
// code goes here
}