ICloneable class with strings
-
Hello, Why is it necessary to duplicate each string with Substring(0) string.clone when copying strings to cloned object? For int you do not need to copy with int.tryparse(x.tostring()) or similar silliness. Simple assignment operator works but somehow not with strings.
-
Hello, Why is it necessary to duplicate each string with Substring(0) string.clone when copying strings to cloned object? For int you do not need to copy with int.tryparse(x.tostring()) or similar silliness. Simple assignment operator works but somehow not with strings.
-
Hello, Why is it necessary to duplicate each string with Substring(0) string.clone when copying strings to cloned object? For int you do not need to copy with int.tryparse(x.tostring()) or similar silliness. Simple assignment operator works but somehow not with strings.
Can you give us an example of what doesn't work? It's not clear from your question exactly what you are trying to do and what you expect to happen.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
-
Can you give us an example of what doesn't work? It's not clear from your question exactly what you are trying to do and what you expect to happen.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
If I clone object A to object B and use an assignment oprator for all the strings in the .Clone(), Changing the string in object B changes it in object A as well
-
If I clone object A to object B and use an assignment oprator for all the strings in the .Clone(), Changing the string in object B changes it in object A as well
Show us the code you are using.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
-
Hello, Why is it necessary to duplicate each string with Substring(0) string.clone when copying strings to cloned object? For int you do not need to copy with int.tryparse(x.tostring()) or similar silliness. Simple assignment operator works but somehow not with strings.
Because, if you look at the specification of String.Clone, it states; "The return value is not an independent copy of this instance; it is simply another view of the same data. Use the Copy or CopyTo method to create a separate String object with the same value as this instance." Read https://msdn.microsoft.com/en-us/library/system.string.clone(v=vs.110).aspx If you also read the specification for; ICloneable Interface (System)[^] it states that ". It does not specify whether the cloning operation performs a deep copy, a shallow copy, or something in between. Nor does it require all property values of the original instance to be copied to the new instance." Hope that helps..........
Dave Find Me On:Web|Facebook|Twitter|LinkedIn Folding Stats: Team CodeProject
-
Because, if you look at the specification of String.Clone, it states; "The return value is not an independent copy of this instance; it is simply another view of the same data. Use the Copy or CopyTo method to create a separate String object with the same value as this instance." Read https://msdn.microsoft.com/en-us/library/system.string.clone(v=vs.110).aspx If you also read the specification for; ICloneable Interface (System)[^] it states that ". It does not specify whether the cloning operation performs a deep copy, a shallow copy, or something in between. Nor does it require all property values of the original instance to be copied to the new instance." Hope that helps..........
Dave Find Me On:Web|Facebook|Twitter|LinkedIn Folding Stats: Team CodeProject
:-O I was using .Substring(0) instead of Copy. And have been looking to know why clone and = do not copy the string. Thank you for the info
-
Show us the code you are using.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
I am working on converting server code from Java to C#.. Not really ASP but pure C# as this serves only text, the frontend server sends my app a request on port 8080 consisting of user's UserAgent and source IP, and my program decides to serve some text for every 1 in 20 or as defined. Its proprietary code
-
If I clone object A to object B and use an assignment oprator for all the strings in the .Clone(), Changing the string in object B changes it in object A as well
WorkOnlineClick2Wealth wrote:
Changing the string in object B changes it in object A as well
How are you "changing" the string, given that strings in .NET are immutable? :confused:
class Foo
{
public string Bar { get; set; }
}Foo a = new Foo { Bar = "Hello" };
Foo b = new Foo { Bar = a.Bar };b.Bar = "Goodbye";
Console.WriteLine(a.Bar);
// Output: "Hello"If you can't show us the code you're using, you need to create a small sample project to reproduce the problem and show us that code instead.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer