private void button3_Click(object sender, EventArgs e)
{
int num1 = 100;
int num2 = 100;
if (object.Equals(num1, num2))
{
MessageBox.Show("Equal");//输出Equal;
}
else
{
MessageBox.Show("Not Equal");
}
People p1 = new People();
People p2 = new People();
if (object.Equals(p1, p2))
{
MessageBox.Show("Person Equal");
}
else
{
MessageBox.Show("Person Not Equal");//默认比较的是两个对象的引用是否相等,由于p1,p2不是指向同一个对象,所以返回Person Not Equal
}
if (object.ReferenceEquals(p1, p2))
{
MessageBox.Show("Person Equal");
}
else
{
MessageBox.Show("Person Not Equal");//默认比较的是两个对象的引用是否相等,由于p1,p2不是指向同一个对象,所以返回Person Not Equal
}
p2 = p1;
if (object.Equals(p1, p2))
{
MessageBox.Show(" Reference Person Equal");//输出Reference Person Equal,由于p1,p2指向同一个对象
}
else
{
MessageBox.Show("Reference Person Not Equal");
}
if (object.ReferenceEquals(p1, p2))
{
MessageBox.Show("Reference Person Equal");//输出Reference Person Equal,由于p1,p2指向同一个对象
}
else
{
MessageBox.Show("Reference Person Not Equal");
}
}
class People
{
public People()
{
}
}