icloneable

Why does String.Clone() returns the original string and not a copy of it?

☆樱花仙子☆ 提交于 2020-12-02 03:40:31
问题 Surprisingly, String.Clone() doesn't return a copy of a string as String.Copy() would do. Instead, it returns 'this' , the original string. I would like to understand why the .Net Framework team choose to go this way. As per MSDN: The ICloneable interface [...] requires that your implementation of the Clone method return a copy of the current object instance. String.Clone() clearly doesn't follow this guideline. I know that strings are immutable, but if immutability was the reason here,

Proper way to implement ICloneable

回眸只為那壹抹淺笑 提交于 2019-12-17 17:36:26
问题 What is the proper way of implementing ICloneable in a class hierarchy? Say I have an abstract class DrawingObject . Another abstract class RectangularObject inherits from DrawingObject . Then there are multiple concrete classes like Shape , Text , Circle etc. that all inherit from RectangularObject . I want to implement ICloneable on DrawingObject and then carry it down the hierarchy, copying available properties at each level and calling parent's Clone at the next level. The problem however

Why should I implement ICloneable in c#?

为君一笑 提交于 2019-12-17 02:11:56
问题 Can you explain to me why I should inherit from ICloneable and implement the Clone() method? If I want to do a deep copy, can't I just implement my method? Let's say MyClone() ? Why should I inherit from ICloneable ? What are the advantages? Is it just a matter of making code "more readable"? 回答1: You shouldn't. Microsoft recommends against implementing ICloneable because there's no clear indication from the interface whether your Clone method performs a "deep" or "shallow" clone. See this

Cloneable in Derived Classes

允我心安 提交于 2019-12-12 12:17:40
问题 Assume I have a class A , and B which derives from A : class A : ICloneable { public object Clone() {...} } class B : A, ICloneable { public object Clone() {...} } which gives 'B.Clone()' hides inherited member 'A.Clone()'. Use the new keyword if hiding was intended. warning. (1) What is the suggested way? using new or declaring A.Clone() as virtual and override in B ? (2) If there are some members in A and properly cloned in A.Clone() , is there an easy way to clone them in B.Clone() or do I

Object deep copy in Silverlight

☆樱花仙子☆ 提交于 2019-12-02 04:55:46
问题 I was trying to create copy of objects in silverligth 5 where interfaces like IFormatters and IcCloanble do not support. * My objects are like this: (Note that these obkjects are obtained on deserializing xml): I tried to do copy like this: [XmlRoot(ElementName = "component")] public class Component { [XmlElement("attributes")] public Attributes Attributes { get; set; } [XmlIgnore] public Attributes atrbtOrginal = new Attributes(); [XmlIgnore] public Attributes atrbtCopy{ get; set; } } public

Object deep copy in Silverlight

て烟熏妆下的殇ゞ 提交于 2019-12-02 00:26:08
I was trying to create copy of objects in silverligth 5 where interfaces like IFormatters and IcCloanble do not support. * My objects are like this: (Note that these obkjects are obtained on deserializing xml): I tried to do copy like this: [XmlRoot(ElementName = "component")] public class Component { [XmlElement("attributes")] public Attributes Attributes { get; set; } [XmlIgnore] public Attributes atrbtOrginal = new Attributes(); [XmlIgnore] public Attributes atrbtCopy{ get; set; } } public Component() { atrbtCopy= atrbtOrginal ; } Sure it will not work then i got this code on seraching on

Proper way to implement ICloneable

99封情书 提交于 2019-11-28 17:45:24
What is the proper way of implementing ICloneable in a class hierarchy? Say I have an abstract class DrawingObject . Another abstract class RectangularObject inherits from DrawingObject . Then there are multiple concrete classes like Shape , Text , Circle etc. that all inherit from RectangularObject . I want to implement ICloneable on DrawingObject and then carry it down the hierarchy, copying available properties at each level and calling parent's Clone at the next level. The problem however is that since the first two classes are abstract, I cannot create their objects in the Clone() method.

Why no ICloneable<T>?

梦想与她 提交于 2019-11-26 17:18:39
Is there a particular reason why a generic ICloneable<T> does not exist? It would be much more comfortable, if I would not need to cast it everytime I clone something. ICloneable is considered a bad API now, since it does not specify whether the result is a deep or a shallow copy. I think this is why they do not improve this interface. You can probably do a typed cloning extension method, but I think it would require a different name since extension methods have less priority than original ones. In addition to Andrey's reply (which I agree with, +1) - when ICloneable is done, you can also

Why should I implement ICloneable in c#?

狂风中的少年 提交于 2019-11-26 11:15:54
Can you explain to me why I should inherit from ICloneable and implement the Clone() method? If I want to do a deep copy, can't I just implement my method? Let's say MyClone() ? Why should I inherit from ICloneable ? What are the advantages? Is it just a matter of making code "more readable"? Matt Hamilton You shouldn't. Microsoft recommends against implementing ICloneable because there's no clear indication from the interface whether your Clone method performs a "deep" or "shallow" clone. See this blog post from Brad Abrams back in 2003(!) for more information. supercat The ICloneable

Why no ICloneable<T>?

泪湿孤枕 提交于 2019-11-26 06:05:17
问题 Is there a particular reason why a generic ICloneable<T> does not exist? It would be much more comfortable, if I would not need to cast it everytime I clone something. 回答1: ICloneable is considered a bad API now, since it does not specify whether the result is a deep or a shallow copy. I think this is why they do not improve this interface. You can probably do a typed cloning extension method, but I think it would require a different name since extension methods have less priority than