🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.csharpbuilder.language.csharp
Article #519

Re: about equal

#519
From: David Clegg
Date: Fri, 12 Nov 2004 01:50
58 lines
3159 bytes
lobster wrote:

> hi,all, what's the difference between
> equals, referenceequals,==?

Object.Equals will tell you whether to object instances should be
considered equal. This may be done by comparing whether the object
references point to the same instance, but it may also be done by
seeing whether two different references have equivilant state or
values. The default implementation in System.Object simply compares if
the references are pointing to the same object.

Object.ReferenceEquals checks whether the two references point to the
same object.

The == operator by default operates like Object.ReferenceEquals, but be
warned that this can be overloaded to provide different behaviour. As a
result it is recommended to use Object.ReferencesEquals if you wish to
check whether two references point to the same object.

> string a = "123";
> string b = "123";
> bool flag = ReferenceEquals(a,b);
> why the flag is true?


Because a & b actually point to the same object. :-)
This is because of an optimization the .NET Framework does called
string interning.

In a nutshell, when the CLR initializes it creates an internal
hashtable where the keys are strings and the values are references to
the strings.

So in the case above, the CLR will add "123" as the key, and the
reference to where this string is stored in the managed heap is stored
as the value. This reference is then stored in the variable a. When the
CLR encounters 'b = "123"', it searches the hashtable using "123" as a
key and stores the value in b (which happens to be the same object
stored in a).

HTH.


-- 
Cheers,
David Clegg
dclegg@gmail.com

Vote 1 http://cc.borland.com/codecentral/ccweb.exe/listing?id=21489 :-)
Now supports Google Groups searching with Dyna-extend(tm) technology!

QualityCentral. The best way to bug Borland about bugs.
http://qc.borland.com

"You never know when an old calendar might come in handy! Sure, it's
not 1985 right now, but who knows what tomorrow will bring?" - Homer
Simpson
Technical Details
Message-ID: <xn0dpoetlboba1006@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!proxad.net!fr.ip.ndsoftware.net!border2.nntp.ams.giganews.com!border1.nntp.ams.giganews.com!nntp.giganews.com!news-hub.cableinet.net!blueyonder!news-peer-test!btnet!newsfeed.bt.es!caladan.landsraad.net!newsgroups.borland.com!not-for-mail
References: <41941ac9@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 1efe35,4ce6d5ccdf90d708
X-Google-Attributes: gid1efe35,public
X-Google-Language: ENGLISH,ASCII-7-bit
From: "David Clegg" <dclegg@gmail.com>
Subject: Re: about equal
Newsgroups: borland.public.csharpbuilder.language.csharp
References:  <41941ac9@newsgroups.borland.com>
Organization: eBet Limited
User-Agent: XanaNews/1.16.4.6
X-Face: d0!4zUWIBj<CO5m0`=Vu"m.2)(~|1-4:I]5n5<OE<hv+kK)Z:n<h&MnW9t_{}>cw&gbV#f>
 mHS4h1mcw[cz:R#J,T;)w/($:2Vl5CQnFP(c\wmN0rSvn25.kmVpCmqb]gj7l9ZP+O4+a+P~L#]4s"
 "Tx^zQ/N"#'/?lV?SHpdR\5(iNj.,I_YMfF=/_3.JWAs3
Message-ID: <xn0dpoetlboba1006@newsgroups.borland.com>
X-Ref: newsgroups.borland.com ~XNS:0000088A
NNTP-Posting-Host: 203.35.87.58
Date: 11 Nov 2004 18:50:55 -0700
X-Trace: newsgroups.borland.com 1100227855 203.35.87.58 (11 Nov 2004 18:50:55 -0700)
Lines: 58
Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!proxad.net!fr.ip.ndsoftware.net!border2.nntp.ams.giganews.com!border1.nntp.ams.giganews.com!nntp.giganews.com!news-hub.cableinet.net!blueyonder!news-peer-test!btnet!newsfeed.bt.es!caladan.landsraad.net!newsgroups.borland.com!not-for-mail