C#基礎:Equals()與運算符==的區(qū)別分析
更新時間:2013年05月22日 17:01:47 作者:
本篇文章是對c#中的Equals()與運算符==的區(qū)別進行了詳細的分析介紹,需要的朋友參考下
對于值類型,如果對象的值相等,則相等運算符 (==) 返回 true,否則返回 false。對于string 以外的引用類型,如果兩個對象引用同一個對象,則 == 返回 true。對于 string 類型,== 比較字符串的值。
==操作比較的是兩個變量的值是否相等。
equals()方法比較的是兩個對象的內(nèi)容是否一致.equals也就是比較引用類型是否是對同一個對象的引用。
對于值類型的比較,這里就不做描述了,下面討論引用類型的比較:
首先我們看一段程序
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Person
{
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
public Person(string name)
{
this.name = name;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string a = new string(new char[] { 'h', 'e', 'l', 'l', 'o' });
string b = new string(new char[] { 'h', 'e', 'l', 'l', 'o' });
Console.WriteLine(a == b);
Console.WriteLine(a.Equals(b));
object g = a;
object h = b;
Console.WriteLine(g == h);
Console.WriteLine(g.Equals(h));
Person p1 = new Person("jia");
Person p2 = new Person("jia");
Console.WriteLine(p1 == p2);
Console.WriteLine(p1.Equals(p2));
Person p3 = new Person("jia");
Person p4 = p3;
Console.WriteLine(p3 == p4);
Console.WriteLine(p3.Equals(p4));
Console.ReadLine();
}
}
}
運行程序,會輸出什么呢?
答案是 true,true,false,true,false,false,true,true。
為什么會出現(xiàn)這個答案呢?因為值類型是存儲在內(nèi)存中的堆棧(以后簡稱棧),而引用類型的變量在棧中僅僅是存儲引用類型變量的地址,而其本身則存儲在堆中。
==操作比較的是兩個變量的值是否相等,對于引用型變量表示的是兩個變量在堆中存儲的地址是否相同,即棧中的內(nèi)容是否相同。
equals操作表示的兩個變量是否是對同一個對象的引用,即堆中的內(nèi)容是否相同。
而字符串是一個特殊的引用型類型,在C#語言中,重載了string 對象的很多方法方法(包括equals()方法),使string對象用起來就像是值類型一樣。
因此在上面的例子中,字符串a(chǎn)和字符串b的兩個比較是相等的。
對于object g 和object h 時內(nèi)存中兩個不同的對象,所以在棧中的內(nèi)容是不相同的,故不相等。而g.equals(h)用的是sting的equals()方法故相等(多太)。如果將字符串a(chǎn)和b作這樣的修改:
string a="aa";
string b="aa";
則,g和h的兩個比較都是相等的。這是因為系統(tǒng)并沒有給字符串b分配內(nèi)存,只是將"aa"指向了b。所以a和b指向的是同一個字符串(字符串在這種賦值的情況下做了內(nèi)存的優(yōu)化)。
對于p1和p2,也是內(nèi)存中兩個不同的對象,所以在內(nèi)存中的地址肯定不相同,故p1==p2會返回false,又因為p1和p2又是對不同對象的引用,所以p1.equals(p2)將返回false。
對于p3和p4,p4=p3,p3將對對象的引用賦給了p4,p3和p4是對同一個對象的引用,所以兩個比較都返回true。
如果我們對person的equals方法重寫:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Person
{
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
public Person(string name)
{
this.name = name;
}
public override bool Equals(object obj)
{
if (!(obj is Person))
return false;
Person per = (Person)obj;
return this.Name == per.Name;
}
}
}
那么p1.equals(p2),就會返回true。
==操作比較的是兩個變量的值是否相等。
equals()方法比較的是兩個對象的內(nèi)容是否一致.equals也就是比較引用類型是否是對同一個對象的引用。
對于值類型的比較,這里就不做描述了,下面討論引用類型的比較:
首先我們看一段程序
復制代碼 代碼如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Person
{
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
public Person(string name)
{
this.name = name;
}
}
}
復制代碼 代碼如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string a = new string(new char[] { 'h', 'e', 'l', 'l', 'o' });
string b = new string(new char[] { 'h', 'e', 'l', 'l', 'o' });
Console.WriteLine(a == b);
Console.WriteLine(a.Equals(b));
object g = a;
object h = b;
Console.WriteLine(g == h);
Console.WriteLine(g.Equals(h));
Person p1 = new Person("jia");
Person p2 = new Person("jia");
Console.WriteLine(p1 == p2);
Console.WriteLine(p1.Equals(p2));
Person p3 = new Person("jia");
Person p4 = p3;
Console.WriteLine(p3 == p4);
Console.WriteLine(p3.Equals(p4));
Console.ReadLine();
}
}
}
運行程序,會輸出什么呢?
答案是 true,true,false,true,false,false,true,true。
為什么會出現(xiàn)這個答案呢?因為值類型是存儲在內(nèi)存中的堆棧(以后簡稱棧),而引用類型的變量在棧中僅僅是存儲引用類型變量的地址,而其本身則存儲在堆中。
==操作比較的是兩個變量的值是否相等,對于引用型變量表示的是兩個變量在堆中存儲的地址是否相同,即棧中的內(nèi)容是否相同。
equals操作表示的兩個變量是否是對同一個對象的引用,即堆中的內(nèi)容是否相同。
而字符串是一個特殊的引用型類型,在C#語言中,重載了string 對象的很多方法方法(包括equals()方法),使string對象用起來就像是值類型一樣。
因此在上面的例子中,字符串a(chǎn)和字符串b的兩個比較是相等的。
對于object g 和object h 時內(nèi)存中兩個不同的對象,所以在棧中的內(nèi)容是不相同的,故不相等。而g.equals(h)用的是sting的equals()方法故相等(多太)。如果將字符串a(chǎn)和b作這樣的修改:
string a="aa";
string b="aa";
則,g和h的兩個比較都是相等的。這是因為系統(tǒng)并沒有給字符串b分配內(nèi)存,只是將"aa"指向了b。所以a和b指向的是同一個字符串(字符串在這種賦值的情況下做了內(nèi)存的優(yōu)化)。
對于p1和p2,也是內(nèi)存中兩個不同的對象,所以在內(nèi)存中的地址肯定不相同,故p1==p2會返回false,又因為p1和p2又是對不同對象的引用,所以p1.equals(p2)將返回false。
對于p3和p4,p4=p3,p3將對對象的引用賦給了p4,p3和p4是對同一個對象的引用,所以兩個比較都返回true。
如果我們對person的equals方法重寫:
復制代碼 代碼如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Person
{
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
public Person(string name)
{
this.name = name;
}
public override bool Equals(object obj)
{
if (!(obj is Person))
return false;
Person per = (Person)obj;
return this.Name == per.Name;
}
}
}
那么p1.equals(p2),就會返回true。
相關文章
C#靜態(tài)代碼織入AOP組件之Rougamo的使用詳解
Rougamo是一個靜態(tài)代碼織入的AOP組件,同為AOP組件較為常用的有Castle、Autofac、AspectCore等,下面就跟隨小編一起來學習一下它的具體使用吧2024-01-01C#使用JavaScriptSerializer序列化時的時間類型處理
這篇文章主要為大家詳細介紹了C#使用JavaScriptSerializer序列化時的時間類型處理,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-08-08C#應用XML作為數(shù)據(jù)庫的快速開發(fā)框架實現(xiàn)方法
這篇文章主要介紹了C#應用XML作為數(shù)據(jù)庫的快速開發(fā)框架實現(xiàn)方法,詳細介紹了將XML作為數(shù)據(jù)庫的C#桌面應用開發(fā)技巧,具有一定的參考借鑒價值,需要的朋友可以參考下2014-12-12C# Double轉(zhuǎn)化為String時的保留位數(shù)及格式方式
這篇文章主要介紹了C# Double轉(zhuǎn)化為String時的保留位數(shù)及格式方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-02-02