close

=====如果要連ID都相等才True的話=====

參考答案:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CSD04
{
    class Program
    {
        static void Main(string[] args)
        {
            Customer customer1 = new Customer() { CustomerID = "1", Name = "Carol", Phone = "888-8888" };
            Customer customer2 = customer1;
            Customer customer3 = (Customer)customer1.Clone();

            Console.WriteLine("customer1 == customer2 ? {0}", customer1 == customer2);
            Console.WriteLine("customer1 == customer3 ? {0}", customer1 == customer3);
            Console.WriteLine("customer1.Equals(customer2) ? {0}", customer1.Equals(customer2));
            Console.WriteLine("customer1.Equals(customer3) ? {0}", customer1.Equals(customer3));

            Console.ReadLine();
        }
    }
}
 

執行結果:

 

 

=====如果ID不同也算是相等的話=====

參考答案:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CSD04
{
    public class Customer 
    {
        public string CustomerID { get; set; }
        public string Name { get; set; }
        public string Phone { get; set; }        
        public Customer Clone()
        {
            Customer temp = new Customer() { CustomerID = this.CustomerID, Name = this.Name, Phone = this.Phone };
            int i = int.Parse(temp.CustomerID);
            temp.CustomerID = (++i).ToString();
            return temp;
        }
        public bool Equals(Customer target)
        {
            if (this.Name == target.Name && this.Phone == target.Phone)
                return true;
            else
                return false;
        }
    }
}
 

 

執行結果:

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 Dino 的頭像
    Dino

    Dino`s Note

    Dino 發表在 痞客邦 留言(0) 人氣()