471,605 Members | 1,605 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,605 software developers and data experts.

System.Object.ReferenceEquals method

hi,
consider the following piece of code:
string s1 = "kiran";

string s3 = s1.Clone() as string;

Console.WriteLine(System.Object.ReferenceEquals(s1 , s3));

The above piece of code gave me true while i was expecting false.

s3 is a clone of s1. so s1 and s3 should be separate objects right?

as a result, one would expect "ReferenceEquals" to return false...

can anyone pls explain what exactly is happening?

regards,

kiran
Feb 6 '06 #1
4 2773
This is exactly the expected behavior! Have a look at the String.Clone()
documentation at
http://msdn2.microsoft.com/en-us/lib...ing.clone.aspx
It states that the String.Clone() method "Returns a reference to this
instance of String"

Sayed Ibrahim Hashimi
www.sedodream.com

"Kiran A K" wrote:
hi,
consider the following piece of code:
string s1 = "kiran";

string s3 = s1.Clone() as string;

Console.WriteLine(System.Object.ReferenceEquals(s1 , s3));

The above piece of code gave me true while i was expecting false.

s3 is a clone of s1. so s1 and s3 should be separate objects right?

as a result, one would expect "ReferenceEquals" to return false...

can anyone pls explain what exactly is happening?

regards,

kiran

Feb 6 '06 #2
Kiran A K,

I suggest reading some information on string interning. If the string is
interned whenever you try to get a reference to the string it will always
return the same reference.
--

Stoitcho Goutsev (100)

"Kiran A K" <ki*****@trigent.com> wrote in message
news:e%****************@TK2MSFTNGP12.phx.gbl...
hi,
consider the following piece of code:
string s1 = "kiran";

string s3 = s1.Clone() as string;

Console.WriteLine(System.Object.ReferenceEquals(s1 , s3));

The above piece of code gave me true while i was expecting false.

s3 is a clone of s1. so s1 and s3 should be separate objects right?

as a result, one would expect "ReferenceEquals" to return false...

can anyone pls explain what exactly is happening?

regards,

kiran

Feb 6 '06 #3
Stoitcho Goutsev (100) wrote:
I suggest reading some information on string interning. If the string is
interned whenever you try to get a reference to the string it will always
return the same reference.


It's not really anything to do with interning. It's arguable that
String.Clone doesn't properly implement ICloneable.Clone, which states
that a reference to a *new* object is returned. However, as has been
pointed out, the documentation for String.Clone itself explains what's
going on.

Any immutable class could implement the same behaviour though, without
having any concept of interning.

Jon

Feb 6 '06 #4
Jon,

Thanks for the clarification.

Frankly I didn't read the docs on this (I should've). I'd expected this
behavior for interned strings and expected the Clone method to return new
copy for not-interned. However you are right that this implemetation could
make sense to any immutable types, even though the name of the method -
Clone, is little bit confusing in this case.
--

Stoitcho Goutsev (100)
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.com...
Stoitcho Goutsev (100) wrote:
I suggest reading some information on string interning. If the string is
interned whenever you try to get a reference to the string it will always
return the same reference.


It's not really anything to do with interning. It's arguable that
String.Clone doesn't properly implement ICloneable.Clone, which states
that a reference to a *new* object is returned. However, as has been
pointed out, the documentation for String.Clone itself explains what's
going on.

Any immutable class could implement the same behaviour though, without
having any concept of interning.

Jon

Feb 6 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

17 posts views Thread by Zeng | last post: by
8 posts views Thread by Ioannis Vranos | last post: by
6 posts views Thread by Daniel Klein | last post: by
8 posts views Thread by Alberto | last post: by
17 posts views Thread by =?Utf-8?B?SmVzcGVyLCBEZW5tYXJr?= | last post: by
16 posts views Thread by DamienS | last post: by
1 post views Thread by XIAOLAOHU | last post: by
reply views Thread by MichaelMortimer | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.