|
ms
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
How to copy an object instead of geting the referenceIn the C# , I want to change the object only after the user click the
"Submit" button , so I first new an object and use the "=" to get the object in memory , I found that the operation "=" only get a reference to the target object which I want to copy , how can I do copy it ? "DogEye" <jipu***@tom.com> ???????/???????? ? ???????? ?????????: news:eXpPquYbFHA.2664@TK2MSFTNGP15.phx.gbl... If the object type supports copying (cloning) it implements the ICloneable> In the C# , I want to change the object only after the user click the > "Submit" button , so I first new an object and use the "=" to get the object > in memory , I found that the operation "=" only get a reference to the > target object which I want to copy , how can I do copy it ? interface. // begin String s1 = "Original string"; String s2 = (String) s1.Clone(); // this must be a copy of the original string s1 = "Another string"; Console.WriteLine(s1); Console.WriteLine(s2); // end Sergei 1. This example is complete nonsense, since a string is a specialised object
and is immutable and therefore just by "referencing" another instance you are effectively making a copy. See thus... string s1 = "two"; string s2 = s1; s1 = "one"; System.Diagnostics.Debug.WriteLine(string.Format("s1 {0}, s2 {1}",s1,s2)); 2. DogEye, Although in essence Sergei's technique for copying an object is correct, please note that available to you are Copy and Clone methods to make shallow and deep copies. Unfortunately (I seem to remember) there is a little inconsistancy of their actions in the Framework - so for more complex objects you should make sure to check the documentation to see what functionality you are getting before assuming it. Br, Mark. Show quoteHide quote "Sergei" <sergei@nospam.summertime.mtu-net.ru> wrote in message news:OA4XqLZbFHA.3240@TK2MSFTNGP12.phx.gbl... > "DogEye" <jipu***@tom.com> ÓÏÏÂÝÉÌ/ÓÏÏÂÝÉÌÁ × ÎÏ×ÏÓÔÑÈ ÓÌÅÄÕÀÝÅÅ: > news:eXpPquYbFHA.2664@TK2MSFTNGP15.phx.gbl... >> In the C# , I want to change the object only after the user click the >> "Submit" button , so I first new an object and use the "=" to get the >> object >> in memory , I found that the operation "=" only get a reference to the >> target object which I want to copy , how can I do copy it ? > > If the object type supports copying (cloning) it implements the ICloneable > interface. > > // begin > String s1 = "Original string"; > String s2 = (String) s1.Clone(); // this must be a copy of the original > string > s1 = "Another string"; > Console.WriteLine(s1); > Console.WriteLine(s2); > // end > > Sergei > On Fri, 10 Jun 2005 12:25:05 +0100, Mark Broadbent wrote:
> 1. This example is complete nonsense, since a string is a specialised Does your example support what you said that 's2 = s1' causes a copy of s1> object and is immutable and therefore just by "referencing" another > instance you are effectively making a copy. See thus... > string s1 = "two"; > string s2 = s1; > s1 = "one"; > System.Diagnostics.Debug.WriteLine(string.Format("s1 {0}, s2 > {1}",s1,s2)); to be made? As in, new memory allocated, content of s1 copied over to new memory, address of new memory written in s2 ? How do we know that s2 isn't still pointing to the memory region initially allocated for s1 ? I would think that only knowing the addresses stored in s1 and s2 would prove what 'referencing' does and what Clone() does. Rico. Rico <ras_***@yahoo.com> wrote:
> > 1. This example is complete nonsense, since a string is a specialised No - in fact a copy isn't being made, but because strings are immutable > > object and is immutable and therefore just by "referencing" another > > instance you are effectively making a copy. See thus... > > string s1 = "two"; > > string s2 = s1; > > s1 = "one"; > > System.Diagnostics.Debug.WriteLine(string.Format("s1 {0}, s2 > > {1}",s1,s2)); > > Does your example support what you said that 's2 = s1' causes a copy of s1 > to be made? it *sort* of looks like it is. Personally I don't like talking about strings as if they were special - they have *some* special handling (for literals, basically) but there aren't many things about them which are different to any other immutable class. > As in, new memory allocated, content of s1 copied over to new memory, Nope.> address of new memory written in s2 ? > How do we know that s2 isn't still pointing to the memory region initially It is, in fact.> allocated for s1 ? > I would think that only knowing the addresses stored in s1 and s2 would In fact, using object.ReferenceEquals you can tell that string.Clone() > prove what 'referencing' does and what Clone() does. just returns "this": using System; public class Test { static void Main() { string x = "hello"; string y = (string)x.Clone(); Console.WriteLine (object.ReferenceEquals(x, y)); } } -- Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet If replying to the group, please do not mail me too Really not sure what you are trying to say?
As my example demonstrates Sergei's use of the string type in this scenario is not valid. As I said in my post since string is immutable, it is impossible for one string to change the an instance of another string. This is fact and is pointless debating, so proving that s1 doesnt point to s2 is not required. You'll also note that I said "referencing another instance you are effectively making a copy" in this string scenario. What I mean by 'effectively' is that even *if* s1 did point to s2 at that time of assignment, the exact moment that a change is made to either s1 or s2 they would point to different instances due to the immutable rule (as my simple example shows). What is happening is not the same as what you say 'As in, new memory allocated, content of s1 copied over to new memory, address of new memory written in s2 ?' to put it simply, the value that is referenced by a string variable is protected from a change from another. br, Mark. Show quoteHide quote "Rico" <ras_***@yahoo.com> wrote in message news:pan.2005.06.11.07.48.02.390000@yahoo.com... > On Fri, 10 Jun 2005 12:25:05 +0100, Mark Broadbent wrote: > >> 1. This example is complete nonsense, since a string is a specialised >> object and is immutable and therefore just by "referencing" another >> instance you are effectively making a copy. See thus... >> string s1 = "two"; >> string s2 = s1; >> s1 = "one"; >> System.Diagnostics.Debug.WriteLine(string.Format("s1 {0}, s2 >> {1}",s1,s2)); > > Does your example support what you said that 's2 = s1' causes a copy of s1 > to be made? > As in, new memory allocated, content of s1 copied over to new memory, > address of new memory written in s2 ? > > How do we know that s2 isn't still pointing to the memory region initially > allocated for s1 ? > > I would think that only knowing the addresses stored in s1 and s2 would > prove what 'referencing' does and what Clone() does. > > Rico. On Sat, 11 Jun 2005 10:34:23 +0100, Mark Broadbent wrote:
> Really not sure what you are trying to say? That you are funny?:) > You'll also note that I said "referencing another instance you The mention of "s2 = s1;"> are effectively making a copy" in this string scenario. What I mean by > 'effectively' is that even *if* s1 did point to s2 at that time of > assignment, the exact moment that a change is made to either s1 or s2 they > would point to different instances due to the immutable rule (as my simple > example shows). 'effectively' making a copy would lead one to think that the subsequent 's1 = "two" ; ' makes the initial object pointed to by s1 eligible for garbage collection, whereas I should think that it is not because it is being pointed to by s2. > to put it simply, the value that is referenced by a string variable is I think it's just more confusing to bring up 'effective copies' to> protected from a change from another. explain that. String is immutable. The content of the object it references cannot be changed. Period. What's up with the new school of thought of some need for protection from "another" ? heh. Rico. Not really sure why you are *still* trying to keep this thread going *but*
this is going to be my last post to it. -See inline comments "Rico" <ras_***@yahoo.com> wrote in message No.news:pan.2005.06.16.04.12.32.360000@yahoo.com... > On Sat, 11 Jun 2005 10:34:23 +0100, Mark Broadbent wrote: >> Really not sure what you are trying to say? > > :) > That you are funny? > Show quoteHide quote >> You'll also note that I said "referencing another instance you Yes s2 does point to this instance, examining the machine code or IL shows >> are effectively making a copy" in this string scenario. What I mean by >> 'effectively' is that even *if* s1 did point to s2 at that time of >> assignment, the exact moment that a change is made to either s1 or s2 >> they >> would point to different instances due to the immutable rule (as my >> simple >> example shows). > > The mention of "s2 = s1;" > 'effectively' making a copy would lead one to think that the subsequent > > 's1 = "two" ; ' > > makes the initial object pointed to by s1 eligible for garbage collection, > whereas I should think that it is not because it is being pointed to by > s2. > this. No, my previous posts were adequate and explanatory. Also the term "effective" is completely fine in this context. "effective" != "actual". >> to put it simply, the value that is referenced by a string variable is I've already explained the immutable concept.>> protected from a change from another. > > I think it's just more confusing to bring up 'effective copies' to > explain that. String is immutable. The content of the object it references > cannot be changed. Period. Also your description "object it references cannot be changed" is incorrect. The referenced location is not safe from changes by pointers. My description *is* correct "to put it simply, the value that is referenced by a string variable is protected from a change from another." > What's up with the new school of thought of some need for protection from ??? Sergei's example was incorrect for this scenario, which he readily > "another" ? heh. admits. Show quoteHide quote > Rico. "Mark Broadbent" <nospam@nospam.com> wrote: Yes, I shouldn't use the String type in the example> 1. This example is complete nonsense, since a string is a specialised object > and is immutable because its Clone() doesn't clone. Sergei and therefore just by "referencing" another instance you Show quoteHide quote > are effectively making a copy. See thus... > string s1 = "two"; > string s2 = s1; > s1 = "one"; > System.Diagnostics.Debug.WriteLine(string.Format("s1 {0}, s2 > {1}",s1,s2)); > > 2. DogEye, Although in essence Sergei's technique for copying an object is > correct, please note that available to you are Copy and Clone methods to > make shallow and deep copies. Unfortunately (I seem to remember) there is a > little inconsistancy of their actions in the Framework - so for more complex > objects you should make sure to check the documentation to see what > functionality you are getting before assuming it. > > Br, > > Mark. DogEye wrote:
> In the C# , I want to change the object only after the user click the Why do you want to copy the object you just created ? So you'd create a new> "Submit" button , so I first new an object and use the "=" to get the object > in memory , I found that the operation "=" only get a reference to the > target object which I want to copy , how can I do copy it ? object and keep a copy of that in memory ? What are you going to do with the original objcet ? In C# there are reference types and value types. Classes are reference types
and structs as well as basic types are value types. If you want to copy a class, you can't use the assignment operator. You must use some other method. Many classes will implement mechanisms to perform this action. The framework defines an interface ICloneable that is implemented by many framework classes. However, ICloneable doesn't dictate weather a deep or shallow copy is made. You will have to look to the documentation for a particular class to see for sure. A deep copy makes full copies of all state information including other objects that are members of the class being copied. In a shallow copy all state is copied but references to member objects are maintained. Show quoteHide quote "DogEye" <jipu***@tom.com> wrote in message news:eXpPquYbFHA.2664@TK2MSFTNGP15.phx.gbl... > In the C# , I want to change the object only after the user click the > "Submit" button , so I first new an object and use the "=" to get the > object > in memory , I found that the operation "=" only get a reference to the > target object which I want to copy , how can I do copy it ? > > Since you ask if the assignment operator makes a copy I am assuming that
you may come from C++. The _key_ concept here is that in C++ objects have _value_ semantics so that the assignment operator may make a copy. In C#, classes have reference semantics. IMHO, it is best to adjust to the use of C# reference semantics and garbage collection. If you want value semantics and if you want copy on assignment behaviour then you can use struct in C#. So to really understand your need, it would be helpful to know why you need a copy of the object, rather than just changing the state of the object using the reference. Strings are immutable objects, so you cannot change the state of a string, but you can create a new string and assign it to an existing reference variable. This is done automagically without a call to new. Regards, Jeff *** Sent via Developersdex http://www.developersdex.com ***
Other interesting topics
Newbie question: Thread access of form components
STA and MTA Covariance in delegates doesn't work with ValueType's? using C# DLL from plain C Hot to... Converting string "2 1/2" to double events and eventargs OnPreRender ??? What is ? C# Eletitlement app - Can this be done Newbie question: Enum in Interface |
|||||||||||||||||||||||