Thanks it is clear to me now.
Main Topics
Browse All TopicsThe result of the below code in Snippet One is as follows:
True
True
False
True
#Snippet One:
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
object c = a;
object d = b;
Console.WriteLine(c == d);
Console.WriteLine (c.Equals (d));
#Snippet One
The result of the below code in Snippet Two
True
True
True
True
#Snippet Two
string a = "hello";
string b = "hello";
Console.WriteLine(a == b);
Console.WriteLine(a.Equals
object c = a;
object d = b;
Console.WriteLine(c == d);
Console.WriteLine (c.Equals (d));
#Snippet Two
My understanding is that the Equals operator in the System.Object compares whether they are referrring to same instance. Please help me to understand why the result in the above two snippets are different though both look similar.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Business Accounts
Answer for Membership
by: testnPosted on 2008-01-01 at 07:50:03ID: 20560241
In the second case, C# compiler has a concept of string pooling to save the compiled code. If you refer to the exact same string in your code, it will actually store only one string in the byte code so both a,b,c,d pointers are pointing to the exact same memory location. So in every case, it will return true. While in the first case, the fact that you use new string(char[]) construct causes the compiler to wait until run-time to construct the string, thus it points to different memory location. The == operator is only overloaded for String data type but not Object data type. So when you upcast it, == operator on Object data type will only compare the reference but not the content in it.