17 Jan 2009 at 16:46 #1 tripitaka tripitaka Associate Joined 8 Nov 2005 Posts 873 Location London I want to compare four objects (actually structs) in C# to see whether or not they are the same. Is there a neater way of doing this than a lengthy if statement? (if a=b & b=c & c=d) Thanks!
I want to compare four objects (actually structs) in C# to see whether or not they are the same. Is there a neater way of doing this than a lengthy if statement? (if a=b & b=c & c=d) Thanks!
17 Jan 2009 at 17:09 #2 Inquisitor Inquisitor Soldato Joined 12 Apr 2004 Posts 11,788 Location Somewhere Just use the Equals method on the value types: Code: Foo value1 = new Foo(1, "bar"); Foo value2 = new Foo(1, "bar"); if (value1.Equals(value2)) // Returns true. { // … } Alternatively, override the == operator (using the Equals method) for the struct in question, allowing a direct comparison. More info here: http://www.jaggersoft.com/pubs/StructsVsClasses.htm#equals behavior
Just use the Equals method on the value types: Code: Foo value1 = new Foo(1, "bar"); Foo value2 = new Foo(1, "bar"); if (value1.Equals(value2)) // Returns true. { // … } Alternatively, override the == operator (using the Equals method) for the struct in question, allowing a direct comparison. More info here: http://www.jaggersoft.com/pubs/StructsVsClasses.htm#equals behavior