2013-02-12 116 views
0

幫助並澄清我...爲什麼發生錯誤?編譯錯誤 - 成員已經定義

class Program 
{ 
    static void Main(string[] args) 
    {  

    } 

    public int GetNames(int id) 
    { 
     return id; 
    } 

    public float GetNames(int id) 
    { 
     return (float)id; 
    } 

    public String GetNames(string id) 
    { 
     return id; 
    } 
} 
+1

你不能有兩種方法具有相同的名字和相同的參數類型。 – Abbas 2013-02-12 12:00:05

+0

看起來像一個控制檯應用程序。您的方法需要** static **,就像您的Main方法一樣。 – Inisheer 2013-02-12 12:01:42

回答

11

您不能擁有具有相同簽名的方法。返回值不是方法簽名的一部分。簽名由方法名稱和輸入參數定義。所以,你有兩種方法具有相同簽名:

GetNames(int) 

Methods MSDN上

製品的方法的返回類型不是方法 對方法重載的目的簽名的一部分。

解決方案 - 讓不同的簽名(重命名方法,方法的改變參數類型,或更改參數號)