[C# Helper]
Index Books FAQ Contact About Rod
[Beginning Database Design Solutions, Second Edition]

[Beginning Software Engineering, Second Edition]

[Essential Algorithms, Second Edition]

[The Modern C# Challenge]

[WPF 3d, Three-Dimensional Graphics with WPF and C#]

[The C# Helper Top 100]

[Interview Puzzles Dissected]

[C# 24-Hour Trainer]

[C# 5.0 Programmer's Reference]

[MCSD Certification Toolkit (Exam 70-483): Programming in C#]

Title:

[]

This example shows how you can overload the == and != operators to make it easier to compare structs.

By default, for reference types (classes), == returns true if two references refer to the same object (reference equality). It returns false if two references refer to different objects that happen to have the same values (value equality).

The article Guidelines for Overloading Equals() and Operator == (C# Programming Guide) says you can override Equals if you like to test value equality, but the article recommends that you leave == alone so it tests reference equality for reference types.

Structs, however, are value types not reference types. A struct's Equals method is defined by default to return value equality and == isn't defined at all so you can't use == to compare two structs.

This example uses the following code to override == for the Person class to check value equality. The Human class is similar but doesn't override ==.

private struct Person { public string FirstName, LastName; public static bool operator ==(Person per1, Person per2) { return per1.Equals(per2); } public static bool operator !=(Person per1, Person per2) { return !per1.Equals(per2); } //public override bool Equals(object obj) //{ // return base.Equals(obj); //} //public override int GetHashCode() //{ // return base.GetHashCode(); //} }; private struct Human { public string FirstName, LastName; };

Both of these classes define FirstName and LastName fields.

The Person class overrides == to make it use the default implementation of Equals to test value equality. It also overrides != to return the negation of ==. Note that == and != come in pairs, so if you override one you must also override the other.

Also note that if you override == and != then Visual Studio warns you if you do not also override Equals and GetHashCode. You can uncomment out the code shown above to make Visual Studio shut up about that.

The following code shows how the program tests == for the Person struct.

private void Form1_Load(object sender, EventArgs e) { Person person1 = new Person() { FirstName = "Rod", LastName = "Stephens" }; Person person2 = new Person() { FirstName = "Rod", LastName = "Stephens" }; Person person3 = new Person() { FirstName = "Ann", LastName = "Archer" }; Human human1 = new Human() { FirstName = "Rod", LastName = "Stephens" }; Human human2 = new Human() { FirstName = "Rod", LastName = "Stephens" }; // Use == for Persons. lstResults.Items.Add("person1 == person2: " + (person1 == person2).ToString()); lstResults.Items.Add("person1 == person3: " + (person1 == person3).ToString()); lstResults.Items.Add("person1 != person2: " + (person1 != person2).ToString()); lstResults.Items.Add("person1 != person3: " + (person1 != person3).ToString()); // Use == for Humans. This isn't allowed. //bool humans_equal = (human1 == human2); //lstResults.Items.Add("human1 == human2: " + humans_equal.ToString()); }

The code makes some Person and Human variables. It then uses == to see whether some of them are equal. The code that compares Human structs is commented out because == is not defined for Human so Visual Studio won't let that code compile.

Download the example to experiment with it and to see additional details.

© 2009-2023 Rocky Mountain Computer Consulting, Inc. All rights reserved.