0

If String with exactly same value are to be optimized to refer to same object then why it is different for below cases:

        //Case 1:        
        String str1 = "name";
        String str2 = "name";

        if (str1 == str2)
            Console.WriteLine("True"); //Output True
        if (object.ReferenceEquals(str1, str2))
            Console.WriteLine("True"); //Output : True

        //Case 2:
        string hello = "hello";
        string helloWorld = "hello world";
        string helloWorld2 = hello + " world";
        if (helloWorld == helloWorld2)
            Console.WriteLine("True"); //Output : True
        if (object.ReferenceEquals(helloWorld, helloWorld2))
            Console.WriteLine("True"); 
        else
            Console.WriteLine("False"); //Output : False

        //Case 3
        helloWorld2 = "hello world";
        if (helloWorld == helloWorld2)
            Console.WriteLine("True"); //Output : True
        if (object.ReferenceEquals(helloWorld, helloWorld2))
            Console.WriteLine("True"); //Output : True
Bhuwan Pandey
  • 514
  • 1
  • 6
  • 19
  • 1
    What would you expect them to return? They are checking the values of two variables, which are equal. – Andrei V Jun 17 '15 at 06:29
  • Can you explain why you think that one or the other shouldn't? Both should return true, so we need to understand why you think that one shouldn't to explain where you have the wrong idea. – TheEvilPenguin Jun 17 '15 at 06:29
  • What is the point of string being Reference Type – Bhuwan Pandey Jun 17 '15 at 06:29
  • [Strings are a special case](http://stackoverflow.com/questions/636932/in-c-why-is-string-a-reference-type-that-behaves-like-a-value-type) – Sayse Jun 17 '15 at 06:30
  • you mean in none of the case it will compare reference of str1 and str2...being a reference type – Bhuwan Pandey Jun 17 '15 at 06:31
  • "What is the point of string being Reference Type": If it wasn't then passing a `String` as a method argument by value, or even assigning from one variable to another, would make a copy of the entire text, which might be thousands of characters long. – jmcilhinney Jun 17 '15 at 06:32
  • Something else to consider is that C# has string interning - different instances of the same string are optimized away to one copy. In most cases, equal strings will share a reference. – TheEvilPenguin Jun 17 '15 at 06:34
  • 1
    I can't really think of any good reason to compare two strings by reference. If you really want to then you can use `Object.ReferenceEquals` but you should know that the example you have shown would result in only one `String` object anyway, as the system would optimise the code. As `String` objects are immutable, what good would two `String` objects with the same text do you anyway? – jmcilhinney Jun 17 '15 at 06:36
  • 1
    Why do you want to distinguish between `String` references in the first place? It's a waste of time to for us to help you do something pointless. – jmcilhinney Jun 17 '15 at 06:37
  • 2
    [Understanding C#: String.Intern makes strings interesting](http://broadcast.oreilly.com/2010/08/understanding-c-stringintern-m.html) – Corak Jun 17 '15 at 06:37
  • Thanks a lot @Corak...This is really helpful. – Bhuwan Pandey Jun 17 '15 at 06:41
  • @BhuwanPandey I don't want to be too dismissive, but it's literally the first Google result for 'c# string interning', not to mention it was already linked by Corak. You need to put in at least some effort before you ask questions here. We aren't here to do your research for you. – TheEvilPenguin Jun 17 '15 at 12:40
  • @TheEvilPenguin Nobody forcing you to reply, if you don't feel like Please don't...Thanks for your great advice – Bhuwan Pandey Jun 17 '15 at 12:51
  • @TheEvilPenguin your name really suits you and Thanks for unnecessary down vote to all my Questions. – Bhuwan Pandey Jun 17 '15 at 12:59
  • @BhuwanPandey I replied to try to improve the quality of the community. The aim of SO is to create a database of questions and answers which will be useful to other programmers, not to relieve you of the need to do even the most basic searching for yourself. This question does not help anyone else (it's difficult to even tell what you want). I didn't downvote all of your questions, I downvoted this one and one other that I saw in the question list which was basically a duplicate of this one. – TheEvilPenguin Jun 17 '15 at 22:50

0 Answers0