1. ReferneceEqual

专用于引用类型的比较

对于值类型返回的永远是false

对于引用类型,会比较他们的引用是否相等,假如相等,返回true,否则返回false

对于string类型,也是比较其引用

static void Main(string[] args)
{
int a = 100;
int b = 100;
Console.WriteLine(ReferenceEquals(a,b)); //false

string c = "dd";
string d = "dd";
Console.WriteLine(ReferenceEquals(c,d)); //True
Console.ReadKey();
}
  1. ==

对于值类型,先比较他们的类型,假如类型不同,就转换成同一类型,然后再比较值

int a = 100;
float b = 100;
WriteLine(a == b); //true

对于引用类型,比较引用,等效于ReferenceEqual

static void Main(string[] args)
{
Student student1 = new Student();
Student student2 = new Student();
WriteLine(student1 == student2); //false
Student student3 = student1;
WriteLine(student1 == student3); //true

}
class Student
{
public string Name { get; set; }
}

对于string==已经被改写,只会比较它们的值是否相等,不会比较引用

string c = "5";
string d = "5";
WriteLine(c == d); //true 值相等

3.Equal
对于值类型,先比较他们的类型,假如类型不同,返回false,假如类型相同则比较值,假如值相同返回true,不同返回false

int a = 100;
float b = 100f;
WriteLine(a.Equals(b)); //false

double c = 500;
double d = 500;
WriteLine(c.Equals(d)); //true

对于引用类型,比较他们的引用是否相等,假如相等,返回true,否则返回false

class Program
{
static void Main(string[] args)
{

Student student1=new Student();
Student student2=new Student();
WriteLine(student1.Equals(student2)); //false
Student student3 = student1;
WriteLine(student3.Equals(student1)); //true
ReadKey();
}
}

class Student
{
public string Name { get; set; }
}

对于string,Equal已经被改写,只会比较它们的值是否相等,不会比较引用

string a = "41";
string b = "41";
WriteLine(a.Equals(b));//true

同样的,string.Equal(a1,a2)也是比较值是否相等

string a = "jj";
string b = "jj";
WriteLine(String.Equals(a,b)); //true