參考答案:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CSD03
{
class Program
{
static void Main(string[] args)
{
var objectA = new myObject { p1 = "1", p2 = "0" };
var objectB = new myObject { p1 = "1", p2 = "1" };
var objectC = new myObject { p1 = "1", p2 = "0" };
if (objectA.Equals(objectB))
Console.WriteLine("objectA 等於 objectB");
else
Console.WriteLine("objectA 不等於 objectB");
if (objectA.Equals(objectC))
Console.WriteLine("objectA 等於 objectC");
else
Console.WriteLine("objectA 不等於 objectC");
if (objectB.Equals(objectC))
Console.WriteLine("objectB 等於 objectC");
else
Console.WriteLine("objectB 不等於 objectC");
Console.ReadLine();
}
}
public class myObject
{
public string p1 { get; set; }
public string p2 { get; set; }
public override bool Equals(object obj)
{
// TODO: 實作比較邏輯。
if (this.p1 == ((myObject)obj).p1 && this.p2 == ((myObject)obj).p2)
return true;
else
return false;
}
}
}
執行結果:
留言列表