CLR如何知道調用哪個方法,因爲它們返回不同的值(一個是無效的,另一個是int)?在重載意義上,這也是不正確的,一種具有不同返回類型的相同參數的方法。C#public void static Main(String [] args){}和public int main(String [] args)兩個重載的方法一起工作嗎?
例如:
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;
}
}
但主要方法是不維持重載規則。
請提供一個代碼示例。 – Kapol