473,406 Members | 2,713 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

what is difference "==" equals and ReferenceEquals

Hi,

take string class as an example, who can explain the difference?

Thanks

Nov 16 '05 #1
3 17613
Fei Li wrote:
Hi,

take string class as an example, who can explain the difference?


In the case of string, == will return true if both strings contain the
same value. ReferenceEquals returns true if both strings are the same
instance. Here is a code example:

string a = new string('a',3);
string b = new string('a',3);
string c = a;
Console.WriteLine(a==b); // True
Console.WriteLine(object.ReferenceEquals(a,b)); // False
Console.WriteLine(a==c); // True
Console.WriteLine(object.ReferenceEquals(a,c)); // True

In the case above, both a and b will contain the value "aaa". However
they are not the same instance. Both a and c will also contain the
value "aaa", but they are also the same instance.
--
Tom Porterfield
Nov 16 '05 #2
Hi Fei Li,

Basically, == tests if object references point to the same object, and Equals() tests of object A has the same content as object B. For strings there isn't really a difference, because both == and Equals have been overriden to compare the content of the string.

Consider this:

public class MyClass1
{
private int i = 0;
public MyClass1(int value)
{
i = value;
}
}

public class MyClass2
{
private int i = 0;
public MyClass2(int value)
{
i = value;
}

public override bool Equals(object obj)
{
if(!(obj is MyClass2))
return false;

MyClass2 mc = (MyClass2)obj;
if(mc.i == this.i)
return true;
else
return false;
}

public override int GetHashCode()
{
return base.GetHashCode ();
}
}

MyClass1 A = new MyClass1(1);
MyClass1 B = new MyClass1(1);
MyClass2 C = new MyClass2(1);
MyClass2 D = new MyClass2(1);

A == B is false because A and B are references to different objects

A.Equals(B) is false because MyClass1 doesn't override Equals, so the base Object.Equals is used, which only compares references (==)

C == D is also false, same == operator, C and D are different references

C.Equals(D) is true because MyClass2 has overridden the default Equals method and implemented its own definition of equality. In this case C equals D if they are both MyClass2 and have the same value of 'i'. GetHashCode needs to be overriden of you override Equals.

string has not only overriden Equals to compare the content of the string, but also == to do the same.

Equals is meaningless unless you give it a meaning of your own.
--
Happy coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #3
Ah, ReferenceEquals, not Equals.

As Tom pointed out, using Equals for strings would not work since it would use the overriden method which compares the content. ReferenceEquals could then be used to determine if the strings really are the same strings and not just the same character sequence.

--
Happy coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Ken Varn | last post by:
Sometimes when I try to close my managed C++ application, the following dialog displays in Win 2000 Pro: The title of the dialog is "Server Busy". The message is "This action cannot be completed...
5
by: Jarek | last post by:
Hi all! I'm optimizing my C++ multi-threaded application (linux). My application consumes huge amout of memory from unknown reason. There are no memory leaks, or other allocation bugs,...
1
by: blue | last post by:
Looking at code from the following: http://www.gamedev.net/reference/programming/features/enginuity2/page3.asp I'm confused though... CMMPointer(T *o) { obj=0; *this=0;
5
by: junky_fellow | last post by:
Each time i submit some pattern to "google", it shows search took 0.XX seconds for exploring millions of web pages. When i look for efficient ways of searching a string, they always say compare...
2
by: Lasse Edsvik | last post by:
Hello I was wondering if you guys could tell me if: cmd.ExecuteReader(CommandBehavior.CloseConnection); returns a dataset, datatable or what?
1
by: Alan Silver | last post by:
Hello, I have the following line of code in a script... litMsg.Text = Server.MapPath("/"); where litMsg is an ASP.Net Literal control. When I try and run this page, I get the error ... ...
6
by: MilanB | last post by:
Hello What "0&" means in this function call? ret = InternetQueryOption(0&, INTERNET_OPTION_CONNECTED_STATE, _ ci, ci_len) Thanks Milan
6
by: John Pass | last post by:
What is the difference between a While and Do While/Loop repetition structure. If they is no difference (as it seems) why do both exist?
9
by: Dullme | last post by:
i can hardly know what is the code for this. I have researched palindromes, but unfortunately, I only have read about integers not for characters..I need some help..
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.