![](/img/trans.png)
[英]What is the difference between „public static void Main()“ and „public static void Main(String[] args)“?
[英]How C# public void static Main(String[] args){} and public int static Main(String[] args) two overloaded method work together?
由于CLR返回不同的值(一个为void而另一个为int),因此CLR如何知道要调用的方法? 从重载的意义上来说,这也是不正确的,因为具有相同参数的方法具有不同的返回类型。
例:
class Program
{
static int Main(String[] args) //Main with int return type but Parameter String[] args
{
return 0;
}
/* this main method also gonna get called by CLR even though return type void and Same parameter String[] args.
static void Main(String[] args) //Main with int return type but String[] args
{
} */
private static void func(int one)
{
Console.WriteLine(one);
}
private static int func(int one) //compiler error. two overloaded method cant have same parameter and different return type.
{
return 1;
}
}
但主要方法是不维护重载规则。
在.NET中,一个可执行文件只能有一个入口点,即只允许一个Main方法。 更具体地说,仅当签名与以下2之一匹配且该方法是静态的时,才将Main方法视为入口点。
如果提供的主方法签名与上述两种方法不同,则不将其视为主方法。 因此,下面的代码是允许的,
class Program
{
static void Main () //Entry point
{
}
static void Main(int number)
{
}
}
以下代码未编译,因为它在两个位置找到了匹配的签名。
class Program
{
static void Main () //Entry point
{
}
static void Main(String[] args) //another entrypoint!!! Compile Error
{
}
}
下面的代码也不会编译,因为根本没有入口点,
class Program
{
static void Main (int a) //Not an Entry point
{
}
static void Main(float b) //Not an entry point
{
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.