compare two structs via ==

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • titan nyquist

    #1

    compare two structs via ==

    I wish to compare two structs via == but it does not compile. I can
    overload and create my own == but am I missing something that c#
    already has implemented?

    ~titan

  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: compare two structs via ==

    No, you have not. If you overload ==, then you have to overload != as
    well.

    On top of that, you should also overload Equals and GetHashCode as well
    to produce consistent results.


    --
    - Nicholas Paldino [.NET/C# MVP]
    - mvp@spam.guard. caspershouse.co m

    "titan nyquist" <titan.nyquist@ gmail.comwrote in message
    news:1180554252 .418734.196230@ z28g2000prd.goo glegroups.com.. .
    >I wish to compare two structs via == but it does not compile. I can
    overload and create my own == but am I missing something that c#
    already has implemented?
    >
    ~titan
    >

    Comment

    • =?ISO-8859-1?Q?G=F6ran_Andersson?=

      #3
      Re: compare two structs via ==

      titan nyquist wrote:
      I wish to compare two structs via == but it does not compile. I can
      overload and create my own == but am I missing something that c#
      already has implemented?
      There is no default comparer for structs, as comparing the values as a
      binary block of data doesn't always make sense.

      If you for example have a struct:

      public struct MyStruct {
      private string _name;
      public MyStruct(string name) { _name = name; }
      public string Name { get { return _name; } }
      }

      If you then create two struct values that contains the same string value:

      MyStruct v1 = new MyStruct("1");
      MyStruct v2 = new MyStruct(1.ToSt ring());

      If you could compare these struct values as binary chunks of data, they
      would not be equal, as the structs contains two different references,
      eventhough the references points to string values that are equal.

      --
      Göran Andersson
      _____
      Göran Anderssons privata hemsida.

      Comment

      • Fabio

        #4
        Re: compare two structs via ==

        "Göran Andersson" <guffa@guffa.co mha scritto nel messaggio
        news:O7I9L9voHH A.2596@TK2MSFTN GP06.phx.gbl...
        >
        There is no default comparer for structs, as comparing the values as a
        binary block of data doesn't always make sense.
        >
        But why I can do this?

        Point p1 = new Point(1, 2);

        Point p2 = new Point(1, 2);

        Console.WriteLi ne(p1 == p2);








        Comment

        • Christof Nordiek

          #5
          Re: compare two structs via ==

          "Fabio" <znt.fabio@virg ilio.itschrieb im Newsbeitrag
          news:uZzjZ02oHH A.3880@TK2MSFTN GP04.phx.gbl...
          "Göran Andersson" <guffa@guffa.co mha scritto nel messaggio
          news:O7I9L9voHH A.2596@TK2MSFTN GP06.phx.gbl...
          >
          >>
          >There is no default comparer for structs, as comparing the values as a
          >binary block of data doesn't always make sense.
          >>
          >
          But why I can do this?
          >
          Point p1 = new Point(1, 2);
          >
          Point p2 = new Point(1, 2);
          >
          Console.WriteLi ne(p1 == p2);
          >
          Because an eqaulity operator is defined for Point in Point itself.

          Christof


          Comment

          • Marc Gravell

            #6
            Re: compare two structs via ==

            There is no *default* equality operator. Point declares one itself -
            look in "reflector" to see it.

            Marc


            Comment

            • Jon Skeet [C# MVP]

              #7
              Re: compare two structs via ==

              On May 31, 11:18 am, "Fabio" <znt.fa...@virg ilio.itwrote:
              There is no default comparer for structs, as comparing the values as a
              binary block of data doesn't always make sense.
              >
              But why I can do this?
              >
              Point p1 = new Point(1, 2);
              Point p2 = new Point(1, 2);
              Console.WriteLi ne(p1 == p2);
              Because Point overloads the equality operator itself, along with
              addition, subtraction etc.

              Jon

              Comment

              • Jon Skeet [C# MVP]

                #8
                Re: compare two structs via ==

                On May 31, 11:32 am, "Jon Skeet [C# MVP]" <s...@pobox.com wrote:

                <snip>

                Oops. Looks like the automatic reply generator for myself, Marc and
                Christof has got out of sync... Time to reboot ourselves again, guys.

                (Anticipating some sort of equivalent reply from Marc and Christof
                now...)

                Jon

                Comment

                • Moty Michaely

                  #9
                  Re: compare two structs via ==

                  On May 31, 1:34 pm, "Jon Skeet [C# MVP]" <s...@pobox.com wrote:
                  On May 31, 11:32 am, "Jon Skeet [C# MVP]" <s...@pobox.com wrote:
                  >
                  <snip>
                  >
                  Oops. Looks like the automatic reply generator for myself, Marc and
                  Christof has got out of sync... Time to reboot ourselves again, guys.
                  >
                  (Anticipating some sort of equivalent reply from Marc and Christof
                  now...)
                  >
                  Jon
                  Hi,

                  This proves what the *asynchronous* fellows mentioned =)

                  [Serializable, StructLayout(La youtKind.Sequen tial),
                  TypeConverter(t ypeof(PointConv erter)), ComVisible(true )]
                  public struct Point
                  {
                  ...
                  public static bool operator ==(Point left, Point right);
                  public static bool operator !=(Point left, Point right);
                  ...
                  }

                  Moty

                  Comment

                  • Mark Wilden

                    #10
                    Re: compare two structs via ==

                    It'd be nice if C# generated a default memberwise equality operator, but I
                    suppose there's a good reason why it doesn't.

                    ///ark


                    Comment

                    • =?ISO-8859-1?Q?G=F6ran_Andersson?=

                      #11
                      Re: compare two structs via ==

                      Mark Wilden wrote:
                      It'd be nice if C# generated a default memberwise equality operator, but I
                      suppose there's a good reason why it doesn't.
                      >
                      Yes, there is.

                      Did you miss my post explaining that a memberwise comparision dosen't
                      always make sense?

                      --
                      Göran Andersson
                      _____
                      Göran Anderssons privata hemsida.

                      Comment

                      • Mark Wilden

                        #12
                        Re: compare two structs via ==

                        "Göran Andersson" <guffa@guffa.co mwrote in message
                        news:exuQzSBpHH A.4696@TK2MSFTN GP02.phx.gbl...
                        Did you miss my post explaining that a memberwise comparision dosen't
                        always make sense?
                        I saw the post from you talking about a bitwise comparison, but not a
                        memberwise comparison. In the latter, you'd compare each member, using its
                        == operator.

                        Did I miss a post?

                        ///ark


                        Comment

                        • =?ISO-8859-1?Q?G=F6ran_Andersson?=

                          #13
                          Re: compare two structs via ==

                          Mark Wilden wrote:
                          "Göran Andersson" <guffa@guffa.co mwrote in message
                          news:exuQzSBpHH A.4696@TK2MSFTN GP02.phx.gbl...
                          >
                          >Did you miss my post explaining that a memberwise comparision dosen't
                          >always make sense?
                          >
                          I saw the post from you talking about a bitwise comparison, but not a
                          memberwise comparison. In the latter, you'd compare each member, using its
                          == operator.
                          >
                          Did I miss a post?
                          >
                          ///ark
                          >
                          Ok, I misunderstood your distinction there. Anyway, not even a
                          memberwise comparison would always make sense.

                          --
                          Göran Andersson
                          _____
                          Göran Anderssons privata hemsida.

                          Comment

                          • Mark Wilden

                            #14
                            Re: compare two structs via ==

                            Memberwise comparison worked pretty well in C++. An object is equal to
                            another object if all its members are equal to the other object's members
                            (using the same criterion for member equality).



                            Comment

                            • =?ISO-8859-1?Q?G=F6ran_Andersson?=

                              #15
                              Re: compare two structs via ==

                              Mark Wilden wrote:
                              Memberwise comparison worked pretty well in C++. An object is equal to
                              another object if all its members are equal to the other object's members
                              (using the same criterion for member equality).
                              >
                              Usually, but not always.

                              I have a faint memory of reading somewhere that the decision to not
                              include an automatic memberwise comparison for structs was that it would
                              automatically add functionality to all structs that you create. If the
                              comparison doesn't make sense for a struct, you would be forced to
                              override it even if it didn't make sense to compare the structs at all.
                              It would not be possible to prevent usage of the comparison at compile
                              time, it could only throw an exception at run time.

                              If you want a memberwise comparison for a struct, it's very easy to
                              write one, and you can most likely make it more efficient than any
                              automatically generated code, as you can choose the order to compare the
                              fields so that you first compare the fields that is most likely to differ.

                              --
                              Göran Andersson
                              _____
                              Göran Anderssons privata hemsida.

                              Comment

                              Working...