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. Object array problem

Object array problem

Scheduled Pinned Locked Moved C#
data-structureshelpquestion
11 Posts 6 Posters 1 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.
  • C Offline
    C Offline
    CrazyCoder26
    wrote on last edited by
    #1

    Hi I have a problem as described below : <pre> object[] obj1={1,"hello"}; object[] obj2={1,"hello"}; if(obj1.Equals(obj2)) { return true; } else { return false; } </pre> According to me, it should return true. But it is returning false (I know I am wrong). But why?????

    CC26

    D B T L V 5 Replies Last reply
    0
    • C CrazyCoder26

      Hi I have a problem as described below : <pre> object[] obj1={1,"hello"}; object[] obj2={1,"hello"}; if(obj1.Equals(obj2)) { return true; } else { return false; } </pre> According to me, it should return true. But it is returning false (I know I am wrong). But why?????

      CC26

      D Offline
      D Offline
      dan sh
      wrote on last edited by
      #2

      Equals method will return true only if both the objects refer to same memory location. Otherwise false. Here obj1 and obj2 are referring to two different memory locations. Hence you are getting return value as false.

      50-50-90 rule: Anytime I have a 50-50 chance of getting something right, there's a 90% probability I'll get it wrong...!!

      C 1 Reply Last reply
      0
      • D dan sh

        Equals method will return true only if both the objects refer to same memory location. Otherwise false. Here obj1 and obj2 are referring to two different memory locations. Hence you are getting return value as false.

        50-50-90 rule: Anytime I have a 50-50 chance of getting something right, there's a 90% probability I'll get it wrong...!!

        C Offline
        C Offline
        CrazyCoder26
        wrote on last edited by
        #3

        Thanks for the reply danish. What if instead of Equal I did if(obj1==obj2)?

        CC26

        D 1 Reply Last reply
        0
        • C CrazyCoder26

          Thanks for the reply danish. What if instead of Equal I did if(obj1==obj2)?

          CC26

          D Offline
          D Offline
          dan sh
          wrote on last edited by
          #4

          That would also give the same result.

          50-50-90 rule: Anytime I have a 50-50 chance of getting something right, there's a 90% probability I'll get it wrong...!!

          1 Reply Last reply
          0
          • C CrazyCoder26

            Hi I have a problem as described below : <pre> object[] obj1={1,"hello"}; object[] obj2={1,"hello"}; if(obj1.Equals(obj2)) { return true; } else { return false; } </pre> According to me, it should return true. But it is returning false (I know I am wrong). But why?????

            CC26

            B Offline
            B Offline
            Blue_Boy
            wrote on last edited by
            #5

            Try this: if (obj1.Length != obj2.Length) { return false; } else { return true; for (int i=0; i < obj1.Length; i++) { if (obj1[i] != obj2[i]) { return false; break; } } }


            I Love T-SQL "Don't torture yourself,let the life to do it for you." If my post helps you kindly save my time by voting my post. www.aktualiteti.com

            V L 2 Replies Last reply
            0
            • C CrazyCoder26

              Hi I have a problem as described below : <pre> object[] obj1={1,"hello"}; object[] obj2={1,"hello"}; if(obj1.Equals(obj2)) { return true; } else { return false; } </pre> According to me, it should return true. But it is returning false (I know I am wrong). But why?????

              CC26

              T Offline
              T Offline
              ThatsAlok
              wrote on last edited by
              #6

              CrazyCoder26 wrote:

              But it is returning false (I know I am wrong).

              Actually you comparing memory location thats why you it return false. consider the case :- When you create object, the memory is created in Heap and it's heap memory reference is stored in stack. So object as such nothing but variable containing address location, so when you compare two object, they bound to differ on memory address, so the answer

              "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
              Never mind - my own stupidity is the source of every "problem" - Mixture

              cheers, Alok Gupta VC Forum Q&A :- I/IV Support CRY- Child Relief and You

              1 Reply Last reply
              0
              • C CrazyCoder26

                Hi I have a problem as described below : <pre> object[] obj1={1,"hello"}; object[] obj2={1,"hello"}; if(obj1.Equals(obj2)) { return true; } else { return false; } </pre> According to me, it should return true. But it is returning false (I know I am wrong). But why?????

                CC26

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

                Both == and Equals perform a reference comparison rather than value comparison. Rewrite your code like this and find out what happens:

                object[] obj1={1,"hello"};
                object[] obj2=obj1;

                1 Reply Last reply
                0
                • C CrazyCoder26

                  Hi I have a problem as described below : <pre> object[] obj1={1,"hello"}; object[] obj2={1,"hello"}; if(obj1.Equals(obj2)) { return true; } else { return false; } </pre> According to me, it should return true. But it is returning false (I know I am wrong). But why?????

                  CC26

                  V Offline
                  V Offline
                  vtchris peterson
                  wrote on last edited by
                  #8

                  If you are wanting to compare the contents of the arrays, then consider the following code:

                    /// <summary>
                    /// Compare 2 arrays for equality.
                    /// </summary>
                    /// <param name="data1">Array 1.</param>
                    /// <param name="data2">Array 2.</param>
                    /// <returns><c>true</c> if equal, <c>false</c> otherwise.</returns>
                    public static bool CompareArrays<T>(T\[\] data1, T\[\] data2)
                    {
                       // If both are null, they're equal
                       if ((data1 == null) && (data2 == null))
                       {
                          return true;
                       }
                       // If either but not both are null, they're not equal
                       if ((data1 == null) || (data2 == null))
                       {
                          return false;
                       }
                       if (data1.Length != data2.Length)
                       {
                          return false;
                       }
                       for (int i = 0; i < data1.Length; i++)
                       {
                          if (!data1\[i\].Equals(data2\[i\]))
                          {
                             return false;
                          }
                       }
                       return true;
                    }
                  
                  1 Reply Last reply
                  0
                  • B Blue_Boy

                    Try this: if (obj1.Length != obj2.Length) { return false; } else { return true; for (int i=0; i < obj1.Length; i++) { if (obj1[i] != obj2[i]) { return false; break; } } }


                    I Love T-SQL "Don't torture yourself,let the life to do it for you." If my post helps you kindly save my time by voting my post. www.aktualiteti.com

                    V Offline
                    V Offline
                    vtchris peterson
                    wrote on last edited by
                    #9

                    Uh, that would return true for any case where obj1 and obj2 are the same length.

                    1 Reply Last reply
                    0
                    • B Blue_Boy

                      Try this: if (obj1.Length != obj2.Length) { return false; } else { return true; for (int i=0; i < obj1.Length; i++) { if (obj1[i] != obj2[i]) { return false; break; } } }


                      I Love T-SQL "Don't torture yourself,let the life to do it for you." If my post helps you kindly save my time by voting my post. www.aktualiteti.com

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

                      A little bit wrong, maybe it should look like this:

                      if (obj1.Length != obj2.Length)
                      {
                      return false;
                      }
                      else
                      {
                      for (int i=0; i < obj1.Length; i++)
                      {
                      if (!object.Equals(obj1[i], obj2[i]))
                      {
                      return false;
                      }
                      }
                      return true;
                      }

                      B 1 Reply Last reply
                      0
                      • L Lost User

                        A little bit wrong, maybe it should look like this:

                        if (obj1.Length != obj2.Length)
                        {
                        return false;
                        }
                        else
                        {
                        for (int i=0; i < obj1.Length; i++)
                        {
                        if (!object.Equals(obj1[i], obj2[i]))
                        {
                        return false;
                        }
                        }
                        return true;
                        }

                        B Offline
                        B Offline
                        Blue_Boy
                        wrote on last edited by
                        #11

                        Yes,you are right,I just couldn't find a bit free time to modify my answer. Thanks for correction.


                        I Love T-SQL "Don't torture yourself,let the life to do it for you." If my post helps you kindly save my time by voting my post. www.aktualiteti.com

                        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