ReferenceEquals()判断两个字符串是否指向相同的内存地址;
Equals,先判断两个字符串有相同的内存位置,则两个字符串相等;否则逐字符比较两个字符串,判断是否相等
下面举一个例子看一下:
view plaincopy to clipboardprint?
string peom1 = "Kubla Khan";
string peom2 = "Kubla Khan";
string peom3 = String.Copy(peom2);
string peom4 = "kubla khan";
//ReferenceEquals()判断两个字符串是否指向相同的内存地址
Console.WriteLine("peom1 == peom2:"+(peom1 == peom2));//True
Console.WriteLine("peom1 == peom3:" + (peom1 == peom3));//True
Console.WriteLine("ReferenceEquals(peom1,peom3):" + ReferenceEquals(peom1, peom3));//False
//Equals,先判断两个字符串有相同的内存位置,则两个字符串相等;否则逐字符比较两个字符串,判断是否相等
Console.WriteLine("Equal(peom1,peom3):" +String.Equals(peom1, peom2));//true
Console.WriteLine("Equal(peom1,peom3):" + String.Equals(peom1, peom3));//true
ReferenceEquals()判断两个字符串是否指向相同的内存地址
最新推荐文章于 2024-07-16 09:45:22 发布