2

I've read this question about using Clone() and want to know if what I'm after will be achieved. My understanding from reading that question is that Clone() does a shallow copy, however reading elsewhere led me to believe differently, and now I'm confused.

Our project has a class Rule that has an ICollection<ICondition> Conditions. We'd like to provide users with a shortcut method to duplicate an existing Condition and modify it rather than start from scratch. To that end, we're providing a Copy To New button.

ICondition looks like this:

interface ICondition
{
    long ID { get; set; }
    string Description { get; set; }
    DateTime EffectiveDate { get; set; }
    string IfStatement { get; set; }
    string PriceVersion { get; set; }
    PriceDetail Pricing { get; set; }
    bool StandardOption { get; set; }
}

Given what I've read about Clone(), I'm fairly confident it would work the way I expect by using ICondition newCondition = conditionToCopy.Clone(), though I'm unsure if Pricing would be duplicated correctly, since it's a complex data type.

So, the first part of my question is, "will this work?" I would just try it and see, however ICondition (or really its underlying Condition) doesn't seem to provide a method for Clone(), which leads me to the second part of my question: can I leverage IClonable to enable this functionality? If so, where?

Is this the way?

public static class Condition : IClonable
{
    ...
    public Condition Clone(Condition conditionToClone)
    {
        return new Condition 
        { 
            Description = this.Description,
            EffectiveDate = this.EffectiveDate,
            IfStatement = this.IfStatement,
            PriceVersion = this.PriceVersion,
            Pricing = this.Pricing,
            StandardOption = this.StandardOption
        }
    }
}

And, given that as the answer, is there any utility to declaring IClonable as the interface? Does it add any value?

Update:

Based on the below answer, I decided to do this, which at least builds (and hopefully runs; haven't tried it yet):

public class Condition
{
    ...
    public Condition Clone()
    {
        return (Condition)base.MemberwiseClone();
    }
}

I still don't see the need for ICloneable, so have left it out. The compiler was complaining about it anyway, something about the base class not being object.

Community
  • 1
  • 1
J.D. Ray
  • 697
  • 1
  • 8
  • 22
  • Your way will work fine. – JWP Mar 06 '15 at 18:35
  • More reading, and I think what I was hoping for was something like `System.Object.MemberwiseClone` (https://msdn.microsoft.com/en-us/library/system.object.memberwiseclone%28v=vs.110%29.aspx), though try as I might, I can't seem to get IntelliSense to offer that as an option. :\ – J.D. Ray Mar 06 '15 at 18:41
  • I too had in the past been confused on deep clones, clones etc. I almost never do it; rather, I use the pattern you show above almost all the time. – JWP Mar 06 '15 at 18:46

1 Answers1

2

There are couple of things. First Condition class should not be static. Second, PriceDetail class [Property pricing], Also need to impleemnt ICloneable. Instance class will allow base.MemberWiseClone() method.

public class Condition : ICloneable
{

    public object Clone()
    {
        base.MemberwiseClone();
    }
}
Gaurav Sharma
  • 586
  • 3
  • 10
  • But what's the usefulness of `ICloneable`? – J.D. Ray Mar 06 '15 at 19:19
  • It is basically for instructing compiler to copy "value" of properties than "reference" as it happens while copying Reference type variable. Creating new instance of a same class and passing same values will do the same thing, but this is more clean. – Gaurav Sharma Mar 06 '15 at 19:22
  • From reading MSDN (https://msdn.microsoft.com/query/dev11.query?appId=Dev11IDEF1&l=EN-US&k=k(System.ICloneable);k(TargetFrameworkMoniker-.NETFramework,Version%3Dv4.5.2);k(DevLang-csharp)&rd=true), as far as I can tell, `ICloneable` simply requires that you implement a method `Clone()` and does nothing else. – J.D. Ray Mar 06 '15 at 19:26
  • Also, should that be `{ return base.MemberwiseClone(); }` ?? – J.D. Ray Mar 06 '15 at 19:29
  • Yes we should return. – Gaurav Sharma Mar 06 '15 at 19:34