デフォルトのEqualsは、2つのオブジェクトが等しいかどうかを判断するのではなく、同じ参照かどうかを判断します.
3832 ワード
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Singleton
{
class Program
{
static void Main(string[] args)
{
// Equals
//Codon p1 = new Codon("dd", 23);
//Codon p2 = new Codon("dd", 23);
//if (p1.Equals(p2))
//{
// Console.Write("Equal");
//}
//else
//{
// Console.Write("Not Equal");// Not Equal
//}
// Equals , :
Codon p1 = new Codon(“dd", 23);
Codon p2 = new Codon("dd", 23);
if (p1.Equals(p2))
{
Console.Write("Equal");// Equal
}
else
{
Console.Write("Not Equal");
}
Console.Read();
}
}
class Codon
{
public string Name { get; set; }
public int Age { get; set; }
public Codon()
{
}
public Codon(string name,int age)
{
this.Name = name;
this.Age = age;
}
public override bool Equals(object obj)
{
Codon codon=obj as Codon;
return this.Name == codon.Name && this.Age == codon.Age;
}
}
}
転載先:https://www.cnblogs.com/ganquanfu2008/archive/2013/04/16/3023904.html