467,905 Members | 1,840 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,905 developers. It's quick & easy.

Does Clone really make a new copy ?

Console.WriteLine(String.Equals(s, s.Clone.ToString))

s is type string.

This returns True.

If Clone really made a new copy I'd expect False.

Wouldn't you?


Mar 7 '07 #1
  • viewed: 2415
Share:
18 Replies
On Mar 6, 7:58 pm, " active" <activeNOS...@a-znet.comwrote:
Console.WriteLine(String.Equals(s, s.Clone.ToString))

s is type string.

This returns True.

If Clone really made a new copy I'd expect False.

Wouldn't you?
Actually, no. If you read the docs on string.equals:
true if the value of the value parameter is the same as this instance;
otherwise, false.

Equals on string has been overloaded to return value equality :)

--
Tom Shelton

Mar 7 '07 #2
It would be important to read the docs on String.Clone() as well. It
doesn't "clone" the string but simply returns another reference. So both
the equality test is True (which one would expect) and the "Is" test is True
(which one would expect wouldn't be the case.)

See: String.Copy() to create a separate object which will pass the equality
test but fail the Is test (demonstrating they aren't the same object.)

"Tom Shelton" <to*********@comcast.netwrote in message
news:11**********************@p10g2000cwp.googlegr oups.com...
On Mar 6, 7:58 pm, " active" <activeNOS...@a-znet.comwrote:
>Console.WriteLine(String.Equals(s, s.Clone.ToString))

s is type string.

This returns True.

If Clone really made a new copy I'd expect False.

Wouldn't you?

Actually, no. If you read the docs on string.equals:
true if the value of the value parameter is the same as this instance;
otherwise, false.

Equals on string has been overloaded to return value equality :)

--
Tom Shelton

Mar 7 '07 #3
active,

In most cases the Clone creates a "shallow" copy. This means that the clone,
copies only the reference.

In fact does mostly "copy" methods the same. There are 2 kinds of copies.
The shallow copy and the deep copy. The last means copy everything. For the
latter you can mostly use CopyTo, although than you change the object as
well. For a real deep copy you have mostly to serialize and to desterilize.

This is not always consistent in Net, by instance the classes from AdoNet
have another behaviour.

Clone means here copy of an empty object (the same as instancing a new
class, but can be handy if you have created everything yourself without a
strongly typed class)

Copy means here really copy of the total object.

Cor

" active" <ac**********@a-znet.comschreef in bericht
news:%2****************@TK2MSFTNGP02.phx.gbl...
Console.WriteLine(String.Equals(s, s.Clone.ToString))

s is type string.

This returns True.

If Clone really made a new copy I'd expect False.

Wouldn't you?


Mar 7 '07 #4
On Mar 6, 9:21 pm, "Tom Leylan" <tley...@nospam.netwrote:
It would be important to read the docs on String.Clone() as well. It
doesn't "clone" the string but simply returns another reference. So both
the equality test is True (which one would expect) and the "Is" test is True
(which one would expect wouldn't be the case.)

See: String.Copy() to create a separate object which will pass the equality
test but fail the Is test (demonstrating they aren't the same object.)
Very true.

Dim s1 As String = "Hello, world!"
Dim s2 As String = DirectCast(s1.Clone(), String)
MessageBox.Show(String.Format("s1.Equals(s2) = {0}, s1 Is s2 =
{1}", _
s1.Equals(s2), s1 Is s2))

Dim s3 As String = String.Copy(s1)
MessageBox.Show(String.Format("s1.Equals(s3) = {0}, s1 Is s3 =
{1}", _
s1.Equals(s3), s1 Is s3))

In both cases, .Equals returns true - but, s1 is not s3 :)

--
Tom Shelton

Mar 7 '07 #5
Thanks for the help.

But isn't "clone" the wrong word to use for coping a reference?
In the rest of the world doesn't clone mean to make a new item, not a new
name?
Thanks

" active" <ac**********@a-znet.comwrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
Console.WriteLine(String.Equals(s, s.Clone.ToString))

s is type string.

This returns True.

If Clone really made a new copy I'd expect False.

Wouldn't you?


Mar 7 '07 #6

"Cor Ligthert [MVP]" <no************@planet.nlwrote in message
news:uI**************@TK2MSFTNGP02.phx.gbl...
active,

In most cases the Clone creates a "shallow" copy. This means that the
clone, copies only the reference.

In fact does mostly "copy" methods the same.
Could you say the above a little differently - I'm not sure what you mean.
Mar 7 '07 #7
Thanks for the help.

But isn't "clone" the wrong word to use for coping a reference?
In the rest of the world doesn't clone mean to make a new item, not a new
name?
Thanks


Mar 7 '07 #8
Thanks for the help.

But isn't "clone" the wrong word to use for coping a reference?
In the rest of the world doesn't clone mean to make a new item, not a new
name?
Thanks


Mar 7 '07 #9
active wrote:
Console.WriteLine(String.Equals(s, s.Clone.ToString))

s is type string.

This returns True.

If Clone really made a new copy I'd expect False.

Wouldn't you?
The purpose of the Clone method is to create a separate copy of the object.

The String class makes an exception to this and returns a copy of the
reference to itself. As the String class is immutable, it doesn't make
any difference if you get a copy of the reference or a recerence to a copy.

--
Göran Andersson
_____
http://www.guffa.com
Mar 7 '07 #10


"Göran Andersson" <gu***@guffa.comwrote in message
news:ug**************@TK2MSFTNGP03.phx.gbl...
active wrote:
>Console.WriteLine(String.Equals(s, s.Clone.ToString))

s is type string.

This returns True.

If Clone really made a new copy I'd expect False.

Wouldn't you?

The purpose of the Clone method is to create a separate copy of the
object.
Actually I've used MenuItem.cloneMenu when I needed a second copy and that
always seemed to work.

>
The String class makes an exception to this and returns a copy of the
reference to itself. As the String class is immutable, it doesn't make any
difference if you get a copy of the reference or a recerence to a copy.
I suppose one might want a copy of a string (maybe delivered by an event for
example) with new protections or scope.

If I want a new reference I would do it with the = sign

Why use clone for that.

So why make an exception - Consistency is very important

I used String.Copy to make the clone
Thanks a lot for straighten it out for me
>
--
Göran Andersson
_____
http://www.guffa.com

Mar 7 '07 #11
active wrote:
>The String class makes an exception to this and returns a copy of the
reference to itself. As the String class is immutable, it doesn't make any
difference if you get a copy of the reference or a recerence to a copy.

I suppose one might want a copy of a string (maybe delivered by an event for
example) with new protections or scope.
The string data doesn't have any scope at all. Only the references to
the data have scope.

The only reason to ever create a copy of a string would be if you want
two strings with the same value but with different references. I however
see no practical use for that at all. What's interresting about a string
is it's value, where the actual data is stored is irrelevant.
If I want a new reference I would do it with the = sign

Why use clone for that.
The Clone method is just implemented that way because there is no reason
to a lot more work than is needed.
So why make an exception - Consistency is very important
The implementation of String.Clone follows the intention of the
interface. It returns a string that won't change if the original string
is assigned a new value.

As string data never change, any number of references can safely point
to the same string data without ever risking that the data is changed by
code elsewhere.
I used String.Copy to make the clone
Why do you want a separate copy of a string anyway? It only wastes memory.
--
Göran Andersson
_____
http://www.guffa.com
Mar 7 '07 #12
Active,
>
But isn't "clone" the wrong word to use for coping a reference?
In the rest of the world doesn't clone mean to make a new item, not a new
name?
I wished it was the only one, however once it is started in use it is
unchangable, what brings us direct on one of the most awtfull ones as
example. Acceptchanges. This does not accept the changes. It changes the
rowstate in a datarow and set that to done.

However the term shallow does describe it maybe nice

http://www.thefreedictionary.com/shallow
Cor
Mar 7 '07 #13
OK

thanks

"Cor Ligthert [MVP]" <no************@planet.nlwrote in message
news:Os**************@TK2MSFTNGP06.phx.gbl...
Active,
>>
But isn't "clone" the wrong word to use for coping a reference?
In the rest of the world doesn't clone mean to make a new item, not a new
name?
I wished it was the only one, however once it is started in use it is
unchangable, what brings us direct on one of the most awtfull ones as
example. Acceptchanges. This does not accept the changes. It changes the
rowstate in a datarow and set that to done.

However the term shallow does describe it maybe nice

http://www.thefreedictionary.com/shallow
Cor

Mar 7 '07 #14
All I know is I have an event that returns a string which I use in creating
a new menu item.

The item does not display.

I do a string.copy to make a new string, use it and the menu item displays
OK

I have no idea why this is true.

When it doesn't display I single step and note that the count shows the item
has been added, but it does not display.

Anyway, the Copy fixed the problem so I guess all the above is now academic.
Thanks for the help

>
Why do you want a separate copy of a string anyway? It only wastes memory.
--
Göran Andersson
_____
http://www.guffa.com

Mar 7 '07 #15
A shallow copy does not mean "copy only the reference". The definition of
the term (in DotNet at least) is that a copy of the "contents" of the
properties is created. In the case of a reference type the contents is a
reference and as such you end up with two objects pointing to some shared
data. Value types would not exhibit this behavior so you in fact have some
sort of hybrid object.

As to why it is called "Clone" that is because the String class implements
ICloneable. The actual implementation is dependent upon what a class
typically does and Goran Andersson has pointed out very nicely why there is
little need to create another "independent" string in order to create a
clone of a string. By their nature strings are immutable and a clone must
be identical so one copy of the value will suffice. Truth be told string
constants are compiled into the app and anything which needs the sequence
"Process Complete" (for instance) is pointing at the one copy of that string
regardless of how many times it appears in the code.

Tom
" active" <ac**********@a-znet.comwrote in message
news:OJ**************@TK2MSFTNGP05.phx.gbl...
>
"Cor Ligthert [MVP]" <no************@planet.nlwrote in message
news:uI**************@TK2MSFTNGP02.phx.gbl...
>active,

In most cases the Clone creates a "shallow" copy. This means that the
clone, copies only the reference.

In fact does mostly "copy" methods the same.
Could you say the above a little differently - I'm not sure what you mean.


Mar 8 '07 #16
active wrote:
Thanks for the help.

But isn't "clone" the wrong word to use for coping a reference?
In the rest of the world doesn't clone mean to make a new item, not a new
name?
Thanks
The purpose of the Clone method is to create a copy that is safe from
changes in the original.

As strings are immutable, the string class can make the exception to
just copy the reference. Eventhough the references point to the same
string data, they can safely do so because the string data never changes.

--
Göran Andersson
_____
http://www.guffa.com
Mar 8 '07 #17
Althought it seems to work at the moment, I think that you should try to
investigate the matter further.

Copying a string is normally never required. The reason that copying the
string makes it work might be because it's causing some side effect that
is affecting something else in the system. The risk is that whatever is
wrong might reappear at any time, or appear somewhere else in the
application.

active wrote:
All I know is I have an event that returns a string which I use in creating
a new menu item.

The item does not display.

I do a string.copy to make a new string, use it and the menu item displays
OK

I have no idea why this is true.

When it doesn't display I single step and note that the count shows the item
has been added, but it does not display.

Anyway, the Copy fixed the problem so I guess all the above is now academic.
Thanks for the help

>Why do you want a separate copy of a string anyway? It only wastes memory.
--
Göran Andersson
_____
http://www.guffa.com
--
Göran Andersson
_____
http://www.guffa.com
Mar 8 '07 #18
I spent over a day looking, lots of single stepping and trying anything I
could think of.

I don't know what else to try.

Thanks for the interest
"Göran Andersson" <gu***@guffa.comwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
Althought it seems to work at the moment, I think that you should try to
investigate the matter further.

Copying a string is normally never required. The reason that copying the
string makes it work might be because it's causing some side effect that
is affecting something else in the system. The risk is that whatever is
wrong might reappear at any time, or appear somewhere else in the
application.

active wrote:
>All I know is I have an event that returns a string which I use in
creating a new menu item.

The item does not display.

I do a string.copy to make a new string, use it and the menu item
displays OK

I have no idea why this is true.

When it doesn't display I single step and note that the count shows the
item has been added, but it does not display.

Anyway, the Copy fixed the problem so I guess all the above is now
academic.
Thanks for the help

>>Why do you want a separate copy of a string anyway? It only wastes
memory.
--
Göran Andersson
_____
http://www.guffa.com

--
Göran Andersson
_____
http://www.guffa.com

Mar 8 '07 #19

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Jason Evans | last post: by
6 posts views Thread by Amil Hanish | last post: by
14 posts views Thread by Hamed | last post: by
1 post views Thread by woessner | last post: by
5 posts views Thread by DavidB | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.