What is wrong with this code? (StringBuilder)

G

Guest

given the following 2 functions:

public bool AnOtherSimpleTest1(string aString) {
StringBuilder strB1 = new StringBuilder();
StringBuilder strB2 = new StringBuilder();
strB1.Append(aString);
strB2.Append(aString);
return strB1.Equals(strB2);
}

public bool AnOtherSimpleTest2(string aString) {
object obj1 = new StringBuilder();
((StringBuilder)obj1).Append(aString);
object obj2 = new StringBuilder();
((StringBuilder)obj2).Append(aString);
return obj1.Equals(obj2);
}

Why returns AnOtherSimpleTest1 TRUE and
AnOtherSimpleTest2 FALSE?
 
G

Guest

Because Object.Equals checks whether the objects are the same but
StringBuilder.Equals returns true if the contents, Capacity, and
MaxCapacity are the same otherwise false.

Overriden to produce different behavoir for the "expected" bahaviour of
"Equals" on that type of object.
 
G

Guest

Because Object.Equals checks whether the objects are the same but
StringBuilder.Equals returns true if the contents, Capacity, and
MaxCapacity are the same otherwise false.

Overriden to produce different behavoir for the "expected" bahaviour of
"Equals" on that type of object.

you almost got it right.

there are actually two overloaded StringBuilder.Equals(). one it inherits
from object that takes object argument. and another one of it's own that
takes StringBuilder as argument.

the keyword here is that it's overloaded, not overriden. If
Object.Equals(object) is truly override by StringBuilder, then you'd expect
the same behavior for both tests, which is not the case as demonstrated.
 
J

jeremiah johnson

The object class and the StringBuilder class have different
implementations of the Equals method, I'm guessing.

I'm more of a Java guy but it looks like Test1 is comparing values,
while Test2 is comparing memory locations, references. if true,
obj1.Equals(obj2) will never return true, unless you copy the reference,
rather than creating two identical objects.

take with the appropriate amount of salt. this is a total guess.
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

From MSDN:

In the first case you are comparing the instance contents, both size of the
buffer and its contents, in the second one you are comparing references.




StringBuilder.Equals Method (StringBuilder) :
*********************************************
Parameters

sb :
An object to compare with this instance or a null reference (Nothing in
Visual Basic).

Return Value
true if this instance and sb have equal string, Capacity, and MaxCapacity
values; otherwise, false.




Object.Equals Method (Object)
*********************************************

Return Value
true if the specified Object is equal to the current Object; otherwise,
false.

Remarks
The default implementation of Equals supports reference equality only, but
derived classes can override this method to support value equality.

For reference types, equality is defined as object equality; that is,
whether the references refer to the same object. For value types, equality
is defined as bitwise equality. The ValueType class supports value types.

Notes to Implementers:

This method can be overridden by a derived class. For example, many of the
base data types return true if both objects represent the same value;
otherwise, false.

This method only compares primitives and objects. It must be overridden to
compare more complex structures, such as arrays of objects.

The following statements must be true for all implementations of the
Equals method. In the list, x, y, and z represent object references that are
not a null reference (Nothing in Visual Basic).




cheers,
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,


jeremiah johnson said:
The object class and the StringBuilder class have different
implementations of the Equals method, I'm guessing.

That's correct and it's clearly stated in the help of object.Equal( ) :

The default implementation of Equals supports reference equality only, but
derived classes can override this method to support value equality.
I'm more of a Java guy but it looks like Test1 is comparing values, while
Test2 is comparing memory locations, references. if true,
obj1.Equals(obj2) will never return true, unless you copy the reference,
rather than creating two identical objects.

Exact.
If you look the MSDN StringBuilder.Equals has two overloads:

Overload List
Returns a value indicating whether this instance is equal to a specified
object.


[C#] public bool Equals(StringBuilder);




Inherited from Object.

Supported by the .NET Compact Framework.


[C#] public virtual bool Equals(object);

take with the appropriate amount of salt. this is a total guess.
And totally acerted :)



cheers,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top