473,397 Members | 2,084 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 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 2849
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: emma middlebrook | last post by:
Hi Having difficulty getting myself clear on how a type's operator== fits in with Object.Equals. Let's just consider reference types. The default operator== tests for object identity...
2
by: Kevin B Ebert | last post by:
Today, I ran into something I didn't quite expect. I'm sure there is a logical explanation for it so I want to post it and see if anyone can explain the difference to me. I came across a...
17
by: Zeng | last post by:
I'm trying to comparing 2 objects (pointer to object) to see if they are the "same" as each other. Here is what the definition of being the "same" object type for both objects, object 1, ...
3
by: Fei Li | last post by:
Hi, take string class as an example, who can explain the difference? Thanks
8
by: Ioannis Vranos | last post by:
In .NET (and C++/CLI) there is an overloaded String == operator for handles. That is when we do comparison of two String handles the contents of the Strings are compared instead of their addresses....
6
by: Daniel Klein | last post by:
A simple question hopefully... Can the Object GetHashCode() method be used to obtain a unique identifier for an instance of an object? To put this another way, what does the...
8
by: Alberto | last post by:
Can you tell me how to read and modify a value in the app.config file using this class? Thank you very much
17
by: =?Utf-8?B?SmVzcGVyLCBEZW5tYXJr?= | last post by:
What is the difference of == and Object.Equals() If I want to query werther to pointers are pointing to same instance, do I use == or Objects.Equals? foo a,b; if ( a == b ) ... or if...
16
by: DamienS | last post by:
In the interests of me saving hair, can someone please explain to me what's going on below? Why doesn't == work in comparing two int's when cast as objects? They're the same type. Note that it...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.