T
tshad
I am looking at the object equals methods and am confused.
I was under the impression that o.equals compares content (not whether they
are the same object as o1==o2 does).
But in my example, this doesn't seem to be happening.
If I have a class Account and I create 2 objects with this class exactly the
same way - the content should be equal (I would think).
Here is the code I am running and all the results make sense to me except
the a3.Equals(a4) which should be true.
**************************************
Account a3 = new Account(1000.0, "Count Dracula", 500);
Account a4 = new Account(1000.0, "Count Dracula", 500);
if (a3 == a4)
Console.WriteLine("a3 == a4");
else
Console.WriteLine("a3 != a4");
if (a3.Equals(a4))
Console.WriteLine("a3.Equals(a4)");
else
Console.WriteLine("a3.NotEquals(a4)");
Console.WriteLine("\na3 content:");
a3.Report();
Console.WriteLine("\na4 content:");
a4.Report();
a3 = a4;
if (a3 == a4)
Console.WriteLine("\nAfter a3=a4 a3==a4");
else
Console.WriteLine("\nAfter a3=a4 a3!=a4");
***************************************
The results:
******************************
a3 != a4
a3.NotEquals(a4) <--- Should be equal as the next set shows
content the same
a3 content:
Account Name: Count Dracula
Balance: 1000
Overdraft Limit: 500
Penalty: 25
a4 content:
Account Name: Count Dracula
Balance: 1000
Overdraft Limit: 500
Penalty: 25
After a3=a4 a3==a4
********************************
Why is a3.Equals(a4) showing as not equal?
Thanks,
Tom
I was under the impression that o.equals compares content (not whether they
are the same object as o1==o2 does).
But in my example, this doesn't seem to be happening.
If I have a class Account and I create 2 objects with this class exactly the
same way - the content should be equal (I would think).
Here is the code I am running and all the results make sense to me except
the a3.Equals(a4) which should be true.
**************************************
Account a3 = new Account(1000.0, "Count Dracula", 500);
Account a4 = new Account(1000.0, "Count Dracula", 500);
if (a3 == a4)
Console.WriteLine("a3 == a4");
else
Console.WriteLine("a3 != a4");
if (a3.Equals(a4))
Console.WriteLine("a3.Equals(a4)");
else
Console.WriteLine("a3.NotEquals(a4)");
Console.WriteLine("\na3 content:");
a3.Report();
Console.WriteLine("\na4 content:");
a4.Report();
a3 = a4;
if (a3 == a4)
Console.WriteLine("\nAfter a3=a4 a3==a4");
else
Console.WriteLine("\nAfter a3=a4 a3!=a4");
***************************************
The results:
******************************
a3 != a4
a3.NotEquals(a4) <--- Should be equal as the next set shows
content the same
a3 content:
Account Name: Count Dracula
Balance: 1000
Overdraft Limit: 500
Penalty: 25
a4 content:
Account Name: Count Dracula
Balance: 1000
Overdraft Limit: 500
Penalty: 25
After a3=a4 a3==a4
********************************
Why is a3.Equals(a4) showing as not equal?
Thanks,
Tom