In my previous post, I was talking about a particular error in one of my company's applications with regard to this issue. Now, I'm going to set out a little code demo to test results using different cases. So, here is my EqualsExample
demo class code as long as two new "MiniBlog
" dummy classes, MiniBlogDummyObject
and MiniBlogDummyObjectOverloaded
, to participate in the test:
MiniBlogDummyObject
is a simple class that just overrides "Equals
" method to set equality regarding "Value
" public
property. It doesn't overload "==
" operator.
public class MiniBlogDummyObject
{
private string _value;
public string Value {
set {_value=value; }
get {return _value;}
}
public MiniBlogDummyObject(string value)
{
_value = value;
}
public override bool Equals(object obj)
{
return this.Value == (obj as MiniBlogDummyObject).Value;
}
public override int GetHashCode()
{
return base.GetHashCode();
}
}
MiniBlogDummyObjectOverloaded
is another class that overrides "Equals
" method to set equality regarding "Value
" public property and overloads "==
" operator comparing values in the same "Value
" property.
public class MiniBlogDummyObjectOverloaded
{
private string _value;
public string Value {
set {_value=value; }
get {return _value;}
}
public MiniBlogDummyObjectOverloaded(string value)
{
_value = value;
}
public override bool Equals(object obj)
{
return this.Value == (obj as MiniBlogDummyObjectOverloaded).Value;
}
public override int GetHashCode()
{
return base.GetHashCode();
}
public static bool operator ==(MiniBlogDummyObjectOverloaded x, MiniBlogDummyObjectOverloaded y)
{
return x.Value == y.Value;
}
public static bool operator !=(MiniBlogDummyObjectOverloaded x, MiniBlogDummyObjectOverloaded y)
{
return x.Value != y.Value;
}
}
Here is the code (and comments with the results) to test four different cases using:
object
type string
type MiniBlogDummyObject
MiniBlogDummyObjectOverloaded
Each case will be tested using two different instances of the same class by comparing them with both "Equals
" method and "==
" operator.
class EqualsExample
{
static void Main(string[] args)
{
string separator = Environment.NewLine + "--------------------------" + Environment.NewLine;
Console.WriteLine("WORKING WITH \"EQUALS\" METHOD AND \"==\" OPERATOR");
Console.WriteLine(separator);
#region Comparison 1
object a1 = new string(new char[] { 'M', 'i', 'n', 'i', 'b', 'l', 'o', 'g' });
object b1 = new string(new char[] { 'M', 'i', 'n', 'i', 'b', 'l', 'o', 'g' });
Console.WriteLine("Comparison 1. \"a1\" and \"b1\" are OBJECT types: " + Environment.NewLine);
Console.WriteLine("a1: {0}", a1.ToString());
Console.WriteLine("b1: {0}", b1.ToString());
Console.WriteLine();
Console.WriteLine("a1 == b1 -> {0}", a1 == b1);
Console.WriteLine("a1.Equals(b1) -> {0}", a1.Equals(b1));
Console.WriteLine(separator);
#endregion
#region Comparison 2
string a2 = new string(new char[] { 'M', 'i', 'n', 'i', 'b', 'l', 'o', 'g' });
string b2 = new string(new char[] { 'M', 'i', 'n', 'i', 'b', 'l', 'o', 'g' });
Console.WriteLine("Comparison 2. \"a2\" and \"b2\" are STRING types: " + Environment.NewLine);
Console.WriteLine("a2: {0}", a2.ToString());
Console.WriteLine("b2: {0}", b2.ToString());
Console.WriteLine();
Console.WriteLine("a2 == b2 -> {0}", a2 == b2);
Console.WriteLine("a2.Equals(b2) -> {0}", a2.Equals(b2));
Console.WriteLine(separator);
#endregion
#region Comparison 3
MiniBlogDummyObject a3 = new MiniBlogDummyObject("MiniBlog");
MiniBlogDummyObject b3 = new MiniBlogDummyObject("MiniBlog");
Console.WriteLine("Comparison 3. \"a3\" and \"b3\" are MiniBlogDummyObject types: " +
Environment.NewLine);
Console.WriteLine("a3: {0}", a3.Value);
Console.WriteLine("b3: {0}", b3.Value);
Console.WriteLine();
Console.WriteLine("a3 == b3 -> {0}", a3 == b3);
Console.WriteLine("a3.Equals(b3) -> {0}", a3.Equals(b3));
Console.WriteLine(separator);
#endregion
#region Comparison 4
MiniBlogDummyObjectOverloaded a4 = new MiniBlogDummyObjectOverloaded("MiniBlog");
MiniBlogDummyObjectOverloaded b4 = new MiniBlogDummyObjectOverloaded("MiniBlog");
Console.WriteLine("Comparison 4. \"a4\" and \"b4\" are MiniBlogDummyObjectOverloaded types: " +
Environment.NewLine);
Console.WriteLine("a4: {0}", a4.Value);
Console.WriteLine("b4: {0}", b4.Value);
Console.WriteLine();
Console.WriteLine("a4 == b4 -> {0}", a4 == b4);
Console.WriteLine("a4.Equals(b4) -> {0}", a4.Equals(b4));
Console.WriteLine(separator);
#endregion
Console.ReadLine();
}
}
As you can see, by running the code above, the results are:
Object
Type
- Comparing with "
==
" operator: False
. It compares references because a1 and b1 are plain object types. So, references are different. - Comparing with "
Equals
" method: True
. It compares underlying values, which are equals.
String
Type
- Comparing with "
==
" operator: True
. "String" class overloads "==" operator, so it compares values, not references - Comparing with "
Equals
" method: True
. It compares underlying values, which are equals.
MiniBlogDummyObject
- Comparing with "
==
" operator: False
. "MiniBlogDummyObject" class doesn't overload "==" operator, so it compares references, not values - Comparing with "
Equals
" method: True. It compares values, which are equals. MiniBlogDummyObjectOverloaded
- Comparing with "
==
" operator: True
. "MiniBlogDummyObjectOverloaded" class overloads "==" operator, so it compares according to that overloaded method, in this case over "Value
" property. - Comparing with "
Equals
" method: True. It compares values, which are equals.
I hope you have a better understanding of this issue. If not, please comment below.
Regards!
Telecommunication Engineer by University of Zaragoza, Spain.
Passionate Software Architect, MCPD, MCAD, MCP. Over 10 years on the way building software.
Mountain Lover, Popular Runner, 3km 8'46, 10km 31'29, Half Marathon 1h08'50, Marathon 2h27'02. I wish I could be able to improve, but It's difficult by now