Does Clone really make a new copy ?

  • Thread starter Thread starter active
  • Start date Start date
A

active

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?
 
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 :)
 
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.)
 
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
 
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 :)
 
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
 
Cor Ligthert said:
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.
 
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
 
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 said:
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 said:
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
 
active said:
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.
 
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
 
OK

thanks

Cor Ligthert said:
Active,
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
 
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
 
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 said:
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.
 
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.
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
 
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 said:
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.
 
Back
Top