Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. C#: How to compare variables by value and reference type

C#: How to compare variables by value and reference type

Scheduled Pinned Locked Moved C#
csharpquestiontutorialperformancehelp
7 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Tridip Bhattacharjee
    wrote on last edited by
    #1

    i have a small confusion that when we compare like == or equal function then both does the same job. u have a very basic question here. see my program full code

    int x = 1000;
    int y = 1000;

        if (x == y)
            Console.WriteLine("Value is same");
        else
            Console.WriteLine("Value is not same");
    
        if (x.Equals(y))
            Console.WriteLine("Value is same");
        else
            Console.WriteLine("Value is not same");
    
        if (object.ReferenceEquals(x,y))
            Console.WriteLine("ref is same");
        else
            Console.WriteLine("ref is not same");
    
    
        string s1 = "Hello";
        string s2 = "Hello";
    
        if (s1 == s2)
            Console.WriteLine("Value is same");
        else
            Console.WriteLine("Value is not same");
    
        if (s1.Equals(s2))
            Console.WriteLine("Value is same");
        else
            Console.WriteLine("Value is not same");
    
        if (object.ReferenceEquals(s1, s2))
            Console.WriteLine("ref is same");
        else
            Console.WriteLine("ref is not same");
    

    i know that this kind of checking if (x == y) is based on value but when i use Equals function then i saw Equals is also work like == operator....am i right ? how to check the reference ?

    if (object.ReferenceEquals(x,y))
    Console.WriteLine("ref is same");
    else
    Console.WriteLine("ref is not same");

    i saw in this case else portion execute........why because different memory is allocated for x and y ? see more for string reference check

    if (object.ReferenceEquals(s1, s2))
    Console.WriteLine("ref is same");
    else
    Console.WriteLine("ref is not same");

    in this scenario s1 and s2 ref found same which is not clear to me because s1 and s2 ref should be different because two are different variable so how s1 and s2 reference check become same? i like to know what are the best process to know value and reference is same or not whatever data type we use may be string or integer or float etc. please some one help me to understand this. one guy said string in dotnet is interned. what is the meaning of In .NET strings are interned ? interned or internal is same ? please discuss with a example just to clarify the meaning of string is interned in dotnet. thanks

    tbhattacharjee

    P L 3 Replies Last reply
    0
    • T Tridip Bhattacharjee

      i have a small confusion that when we compare like == or equal function then both does the same job. u have a very basic question here. see my program full code

      int x = 1000;
      int y = 1000;

          if (x == y)
              Console.WriteLine("Value is same");
          else
              Console.WriteLine("Value is not same");
      
          if (x.Equals(y))
              Console.WriteLine("Value is same");
          else
              Console.WriteLine("Value is not same");
      
          if (object.ReferenceEquals(x,y))
              Console.WriteLine("ref is same");
          else
              Console.WriteLine("ref is not same");
      
      
          string s1 = "Hello";
          string s2 = "Hello";
      
          if (s1 == s2)
              Console.WriteLine("Value is same");
          else
              Console.WriteLine("Value is not same");
      
          if (s1.Equals(s2))
              Console.WriteLine("Value is same");
          else
              Console.WriteLine("Value is not same");
      
          if (object.ReferenceEquals(s1, s2))
              Console.WriteLine("ref is same");
          else
              Console.WriteLine("ref is not same");
      

      i know that this kind of checking if (x == y) is based on value but when i use Equals function then i saw Equals is also work like == operator....am i right ? how to check the reference ?

      if (object.ReferenceEquals(x,y))
      Console.WriteLine("ref is same");
      else
      Console.WriteLine("ref is not same");

      i saw in this case else portion execute........why because different memory is allocated for x and y ? see more for string reference check

      if (object.ReferenceEquals(s1, s2))
      Console.WriteLine("ref is same");
      else
      Console.WriteLine("ref is not same");

      in this scenario s1 and s2 ref found same which is not clear to me because s1 and s2 ref should be different because two are different variable so how s1 and s2 reference check become same? i like to know what are the best process to know value and reference is same or not whatever data type we use may be string or integer or float etc. please some one help me to understand this. one guy said string in dotnet is interned. what is the meaning of In .NET strings are interned ? interned or internal is same ? please discuss with a example just to clarify the meaning of string is interned in dotnet. thanks

      tbhattacharjee

      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #2

      Do not feed the help vampire[^]

      This space for rent

      OriginalGriffO 1 Reply Last reply
      0
      • T Tridip Bhattacharjee

        i have a small confusion that when we compare like == or equal function then both does the same job. u have a very basic question here. see my program full code

        int x = 1000;
        int y = 1000;

            if (x == y)
                Console.WriteLine("Value is same");
            else
                Console.WriteLine("Value is not same");
        
            if (x.Equals(y))
                Console.WriteLine("Value is same");
            else
                Console.WriteLine("Value is not same");
        
            if (object.ReferenceEquals(x,y))
                Console.WriteLine("ref is same");
            else
                Console.WriteLine("ref is not same");
        
        
            string s1 = "Hello";
            string s2 = "Hello";
        
            if (s1 == s2)
                Console.WriteLine("Value is same");
            else
                Console.WriteLine("Value is not same");
        
            if (s1.Equals(s2))
                Console.WriteLine("Value is same");
            else
                Console.WriteLine("Value is not same");
        
            if (object.ReferenceEquals(s1, s2))
                Console.WriteLine("ref is same");
            else
                Console.WriteLine("ref is not same");
        

        i know that this kind of checking if (x == y) is based on value but when i use Equals function then i saw Equals is also work like == operator....am i right ? how to check the reference ?

        if (object.ReferenceEquals(x,y))
        Console.WriteLine("ref is same");
        else
        Console.WriteLine("ref is not same");

        i saw in this case else portion execute........why because different memory is allocated for x and y ? see more for string reference check

        if (object.ReferenceEquals(s1, s2))
        Console.WriteLine("ref is same");
        else
        Console.WriteLine("ref is not same");

        in this scenario s1 and s2 ref found same which is not clear to me because s1 and s2 ref should be different because two are different variable so how s1 and s2 reference check become same? i like to know what are the best process to know value and reference is same or not whatever data type we use may be string or integer or float etc. please some one help me to understand this. one guy said string in dotnet is interned. what is the meaning of In .NET strings are interned ? interned or internal is same ? please discuss with a example just to clarify the meaning of string is interned in dotnet. thanks

        tbhattacharjee

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        == Operator (C#)[^].

        1 Reply Last reply
        0
        • T Tridip Bhattacharjee

          i have a small confusion that when we compare like == or equal function then both does the same job. u have a very basic question here. see my program full code

          int x = 1000;
          int y = 1000;

              if (x == y)
                  Console.WriteLine("Value is same");
              else
                  Console.WriteLine("Value is not same");
          
              if (x.Equals(y))
                  Console.WriteLine("Value is same");
              else
                  Console.WriteLine("Value is not same");
          
              if (object.ReferenceEquals(x,y))
                  Console.WriteLine("ref is same");
              else
                  Console.WriteLine("ref is not same");
          
          
              string s1 = "Hello";
              string s2 = "Hello";
          
              if (s1 == s2)
                  Console.WriteLine("Value is same");
              else
                  Console.WriteLine("Value is not same");
          
              if (s1.Equals(s2))
                  Console.WriteLine("Value is same");
              else
                  Console.WriteLine("Value is not same");
          
              if (object.ReferenceEquals(s1, s2))
                  Console.WriteLine("ref is same");
              else
                  Console.WriteLine("ref is not same");
          

          i know that this kind of checking if (x == y) is based on value but when i use Equals function then i saw Equals is also work like == operator....am i right ? how to check the reference ?

          if (object.ReferenceEquals(x,y))
          Console.WriteLine("ref is same");
          else
          Console.WriteLine("ref is not same");

          i saw in this case else portion execute........why because different memory is allocated for x and y ? see more for string reference check

          if (object.ReferenceEquals(s1, s2))
          Console.WriteLine("ref is same");
          else
          Console.WriteLine("ref is not same");

          in this scenario s1 and s2 ref found same which is not clear to me because s1 and s2 ref should be different because two are different variable so how s1 and s2 reference check become same? i like to know what are the best process to know value and reference is same or not whatever data type we use may be string or integer or float etc. please some one help me to understand this. one guy said string in dotnet is interned. what is the meaning of In .NET strings are interned ? interned or internal is same ? please discuss with a example just to clarify the meaning of string is interned in dotnet. thanks

          tbhattacharjee

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          // note: x and y are instances of value types
          if (object.ReferenceEquals(x, y))
          Console.WriteLine("ref is same");
          else
          Console.WriteLine("ref is not same");

          In this context that will always be not the same, even object.ReferenceEquals(x, x). object.ReferenceEquals takes objects as parameters, so if you pass in a struct (including most primitive types) it will have to be boxed, giving you a new box. Every time. No two new boxes are ever the same, because they're new. If you had boxed them before and then pass the same box twice, of course the same box is the same as the same box. That sounds more confusing than it is.

          Tridip Bhattacharjee wrote:

          how to check the reference ?

          You don't in this case. There is no reference, you just have a couple of ints there. You can compare the references to boxed versions of ints, as you did, but it's not very useful. String is a reference type though, so there's no boxing involved. Different strings, of course, will definitely have different references. Equal strings may also have different references, depending on how they were created. The default == and Equals for reference types is reference equality, but string overrides Equals and implements == to mean value-equality (unlike Java, where String does override Equals, but cannot implement == because that's not a thing you can do in Java). This can come together to cause some fun when looking at both reference equality (by casting to object or using object``.ReferenceEquals) and the usual value-equality for some strings.

          Tridip Bhattacharjee wrote:

          s1 and s2 ref should be different because two are different variable

          Well, that's never how reference equality works. You can have the same reference in however many places you want, for example in

              string s1 = "Hello";
              string s2 = s1;
          

          is it clear that s1 and s2 not only refer to strings with the same contents, but the same string object, and that would be the case for any reference type (but not value types, for which reference equality is useless to even talk about). Possibly you hav

          P 1 Reply Last reply
          0
          • L Lost User

            // note: x and y are instances of value types
            if (object.ReferenceEquals(x, y))
            Console.WriteLine("ref is same");
            else
            Console.WriteLine("ref is not same");

            In this context that will always be not the same, even object.ReferenceEquals(x, x). object.ReferenceEquals takes objects as parameters, so if you pass in a struct (including most primitive types) it will have to be boxed, giving you a new box. Every time. No two new boxes are ever the same, because they're new. If you had boxed them before and then pass the same box twice, of course the same box is the same as the same box. That sounds more confusing than it is.

            Tridip Bhattacharjee wrote:

            how to check the reference ?

            You don't in this case. There is no reference, you just have a couple of ints there. You can compare the references to boxed versions of ints, as you did, but it's not very useful. String is a reference type though, so there's no boxing involved. Different strings, of course, will definitely have different references. Equal strings may also have different references, depending on how they were created. The default == and Equals for reference types is reference equality, but string overrides Equals and implements == to mean value-equality (unlike Java, where String does override Equals, but cannot implement == because that's not a thing you can do in Java). This can come together to cause some fun when looking at both reference equality (by casting to object or using object``.ReferenceEquals) and the usual value-equality for some strings.

            Tridip Bhattacharjee wrote:

            s1 and s2 ref should be different because two are different variable

            Well, that's never how reference equality works. You can have the same reference in however many places you want, for example in

                string s1 = "Hello";
                string s2 = s1;
            

            is it clear that s1 and s2 not only refer to strings with the same contents, but the same string object, and that would be the case for any reference type (but not value types, for which reference equality is useless to even talk about). Possibly you hav

            P Offline
            P Offline
            Pete OHanlon
            wrote on last edited by
            #5

            You're encouraging a help vampire. This user has been told, many times, that he should be able to do basic research for himself. If you look at his message history, you will see dozens of questions like this in this forum.

            This space for rent

            1 Reply Last reply
            0
            • P Pete OHanlon

              Do not feed the help vampire[^]

              This space for rent

              OriginalGriffO Online
              OriginalGriffO Online
              OriginalGriff
              wrote on last edited by
              #6

              What gets me is that he's been here for 13 years, and he still knows nothing about development, and can't use Google ... :sigh:

              Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!

              "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
              "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

              L 1 Reply Last reply
              0
              • OriginalGriffO OriginalGriff

                What gets me is that he's been here for 13 years, and he still knows nothing about development, and can't use Google ... :sigh:

                Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                "Artificial" intelligence.

                "(I) am amazed to see myself here rather than there ... now rather than then". ― Blaise Pascal

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups