1

Edit:

Why this question is not duplicate? I'm not asking difference between .Equals() and ==. I'm asking how does actually == work. I mean, when I created strings using different method, I should have seen different result. But I see same result.

I was looking into == operator in c#. To my surprise it gave same result for following codes (contrary to JAVA). But according to this, == is for reference check and I should see different result for my code then why do I see same result for both of them? Shouldn't I see different results for my piece of codes? Is it because new String() in c# doesn't generate new reference?

 String s = "abc";
 String s1 = "abc";
 Console.WriteLine("Expected output: True, Actual output: " + (s1==s).ToString());

Output

Expected output: True, Actual output: True

Another code check

    String s2 = new String("abc".ToCharArray());
    String s3 = new String("abc".ToCharArray());
    Console.WriteLine("Expected output: False, Actual output: " + (s2 == s3).ToString());

Output

Expected output: False, Actual output: True

Note: I understand difference reference & value check. I've tried result with ReferenceEquals and it shows expected result to me.

Community
  • 1
  • 1
Abhishek
  • 6,912
  • 14
  • 59
  • 85
  • If duplicate does not cover your problem make sure to check results of search http://www.bing.com/search?q=java+c%23+string+equals to see if there is better duplicate. – Alexei Levenkov May 21 '15 at 04:49
  • @AlexeiLevenkov I've edited my question to show, why it is not a duplicate of other SO question. Please look into it :) – Abhishek May 21 '15 at 04:54
  • @Abhishek - In both cases you create the string `"abc"`, and compare `"abc"=="abc"`. Why are you expecting `False` on the second example? – Kobi May 21 '15 at 04:57
  • @Kobi I created new object by specifying `new String()`. That is how it works in JAVA. But as Nikhil pointed out, `==` are overloaded I would see same result. :) – Abhishek May 21 '15 at 04:59
  • 1
    Sure... also questions that have immediate answer in documentation are ... not exactly the best once - [C# string](https://msdn.microsoft.com/en-us/library/362314fe.aspx) "Although string is a reference type, the equality operators (== and !=) are defined to compare the values of string objects, not references. This makes testing for string equality more intuitive" – Alexei Levenkov May 21 '15 at 05:00
  • Thanks, got it. There were many iterations on this question, I might have missed a step. – Kobi May 21 '15 at 05:00
  • @AlexeiLevenkov Yup, got to know about it from Nikhil's answer. I wasn't aware of `==` being overloaded in c#. – Abhishek May 21 '15 at 05:02

2 Answers2

7

Normally for reference types == operator do check for reference equality.

String is also a reference type but in String class == operator is overloaded to check for content equality and not reference equality.

It says and I quote

Determines whether two specified strings have the same value.

Read here https://msdn.microsoft.com/en-us/library/system.string.op_equality(v=vs.110).aspx

FYI, in string != operator is also oveloaded to check for string content inequality.

Nikhil Agrawal
  • 47,018
  • 22
  • 121
  • 208
1

In c# operator == for string compares value, not reference.

x2.
  • 9,554
  • 6
  • 41
  • 62