2014-03-05 86 views
0

我試圖倍率Equals方法在C#和我收到錯誤等倍率Equals方法在C#

SoftCherry.Customer」是‘類型’,但使用類似‘變量’

錯誤行返回this.FirstName ==((客戶)的obj).FirstName & & this.LastName ==((客戶)的obj).LastName;在客戶類

,這是我的代碼

using System; 

    namespace SoftCherry 
    { 
     public class MainClass 
     { 
      public static void Main() 
      { 
       Customer C1 = new Customer(); 
       C1.FirstName = "Raja"; 
       C1.LastName = "Thananki"; 

       Customer C2= new Customer(); 
       C2.FirstName = "Raja"; 
       C2.LastName = "Thananki"; 

       Console.WriteLine(C1== C2); 
       Console.WriteLine(C1.Equals(C2)); 
       Console.ReadLine(); 
      } 
     } 

     public class Customer 
     { 
      public string FirstName { get; set; } 
      public string LastName { get; set; } 

      public override bool Equals(object obj) 
      { 
       if (obj == null) 
       { 
        return false; 
       } 
       if (!(obj is Customer)) 
       { 
        return false; 
       } 
       return this.FirstName == ((Customer).obj).FirstName && 
        this.LastName == ((Customer).obj).LastName; 
      } 

      public override int GetHashCode() 
      { 
       return this.FirstName.GetHashCode()^this.LastName.GetHashCode(); 
      } 
     } 
    } 
+2

丟失'obj'前的小圓點。 – Jon

回答

3

類型後刪除點:

return this.FirstName == ((Customer)obj).FirstName && 
     this.LastName == ((Customer)obj).LastName; 

否則是無效的語法。

+0

非常感謝您的工作時間。 – CodeingJoe

+1

隨時接受任何幫助你的答案。 –

0

您的代碼應該使用(Customer)obj而不是(Customer).obj。在你演出這個劇組的任何地方你都有一個錯誤的小點。

+0

非常感謝您的時間它的工作..! – CodeingJoe