473,569 Members | 2,692 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Does Clone really make a new copy ?

Console.WriteLi ne(String.Equal s(s, s.Clone.ToStrin g))

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
18 2654
On Mar 6, 7:58 pm, " active" <activeNOS... @a-znet.comwrote:
Console.WriteLi ne(String.Equal s(s, s.Clone.ToStrin g))

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*********@co mcast.netwrote in message
news:11******** **************@ p10g2000cwp.goo glegroups.com.. .
On Mar 6, 7:58 pm, " active" <activeNOS... @a-znet.comwrote:
>Console.WriteL ine(String.Equa ls(s, s.Clone.ToStrin g))

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******** ********@TK2MSF TNGP02.phx.gbl. ..
Console.WriteLi ne(String.Equal s(s, s.Clone.ToStrin g))

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.C lone(), 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******** ********@TK2MSF TNGP02.phx.gbl. ..
Console.WriteLi ne(String.Equal s(s, s.Clone.ToStrin g))

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******** ******@TK2MSFTN GP02.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.WriteLi ne(String.Equal s(s, s.Clone.ToStrin g))

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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
2656
by: Jason Evans | last post by:
Hi All, I am writing my own implementation of queue via a linked list, note not a LinkedList, and was running into trouble with the clone method. I was wondering if anyone could point out some not so obvious errors I have made.. Cheers
6
12889
by: Amil Hanish | last post by:
I have two classes that I have implemented ICloneable. From my top-level class, how to I clone the base class values: See "how do I clone the base class values" below. Since Clone returns an object, what do I assign it to in my derived class? public class Base : ICloneable { .... public object Clone() {
6
2142
by: Steve | last post by:
I can't find a straight answer on what to use? I need a deep copy, so I implemented IConeable and the Clone() method. However, I'm not sure I did it correct. Is it suposed to be an allocation of a new object, then the assignment of each member? Is that all or is there something else I need to do? Here is my code <code> public Object...
16
2492
by: Hamed | last post by:
Hello I am developing a utility to be reused in other programs. It I have an object of type Control (a TextBox, ComboBox, etc.) that other programmers use it in applications. they may set some of properties or assign event handlers. I need to be able to clone the manipulated control at runtime. I could use the Clone method of some...
14
8587
by: Hamed | last post by:
Hello It seems that I should implement ICloneable to implement my own clone object. the critical point for me is to make a control object based on another control object that all of its event handlers are set like the old one. Is there a way to do this job? For example, is there a way to use EventInfo object to get all event handlers of...
1
2499
by: woessner | last post by:
I have a lot of classes which derive from a common base class. I want to be able to clone objects of these types so I have given the base class a pure virtual clone method. The derived classes all have complete copy constructors so the cloning process will consiste of "return new Derived(*this);". Here's the setup: struct Base {...
7
2321
by: mavigozler | last post by:
IE7 does not appear to set an event on contained text inside SPAN elements whose 'onclick', 'onmouseover', and 'onmouseout' events, defying the HTML recommendation. Firefox appears to conform. Is that so?
5
1858
by: DavidB | last post by:
I have a situation where a user needs to clone an existing BE database (stored on a server) to his local machine. Assume the followings... User goes into the database FE which has linked BE. BE is stroed on a server. User needs to be able to click a button on the FE that will clone the copy of the BE on the server to C:\. If a copy of...
5
2092
by: raylopez99 | last post by:
In C++, you have symbolic overriding of "+", "=", etc, which C# also has. This question is not really about that. Rather, in C#, when you say: MyObject X = new MyObject(); MyObject Y = new MyObject(); X = Y; //what does this '=' mean?
0
7698
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8122
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7970
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6284
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5513
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5219
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3653
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3640
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.