Hello!
I tested these and found that Programmer was written.
This method Equals is comparing references as default but for this instance
tb which has been cast to TextBox has overriden this I assume in
the same way as == compare strings for equality
This literal string is placed somewhere in memory and a reference is
pointing to it. We call this ref_1
The other object tb is also a referenc to an TextBox object.We call this
ref_2.
If this ref_1 is the same as ref_2 then they are refering to the same
object.
But because Equal for TextBox don't compare references it compare the
value(string)
they are the same.
Have I understodd this correct ?
if (tb.Text.Equals("Programmer"))
{
Console.WriteLine("Programmer");
}
//Tony
"Jon Skeet [C# MVP]" <sk***@pobox.comskrev i meddelandet
news:d8**********************************@d45g2000 hsc.googlegroups.com...
On Jun 25, 8:14 am, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
That said, for strings I'd always just use the == operator. It just seems
more readable to me. Ironically, I think that for some people the
opposite would be true. They would feel that an English word is more
clear than a typographical symbol. To each his own. :)
Slightly aside from Tony's original point, but...
There's one other important thing to think about when it comes to
string equality - what *kind* of string equality you want.
If you use a == b or just a.Equals(b) it will use ordinal comparison -
in other words, if the sequence of chars in a isn't exactly the same
as in b, it will return false.
If you want cultural equivalence or anything similar, you need the
overload of Equals which accepts a StringComparison.
See
http://msdn.microsoft.com/en-us/library/ms973919.aspx for much
more detail and advice.
Jon