0

I have properties as below;

public class Employer
{
public int IDNumber { get; set; }
public DateTime RegDate { get; set; }
public List<BioData> BioInfo { get; set; }
}

BioData.cs

 public class BioData
    {
        public int IndexID { get; set; }
        public string Description { get; set; }
        public int Age { get; set; }
        public double Salary { get; set; }
    }

I want to Deep Clone Employer List including all the fields and properties to the same Employer list.

jps
  • 20,041
  • 15
  • 75
  • 79
Supun Silva
  • 580
  • 5
  • 21

2 Answers2

3

One approach is to provide a copy-constructor for BioData. You should also seal the class, so you don't need to worry about handling copying derived class data:

public sealed class BioData
{
    public BioData()
    {
        // Initialise members or not, as you like.
    }

    public BioData(BioData other)
    {
        IndexID     = other.IndexID;
        Description = other.Description;
        Age         = other.Age;
        Salary      = other.Salary;
    }

    public int    IndexID     { get; set; }
    public string Description { get; set; }
    public int    Age         { get; set; }
    public double Salary      { get; set; }
}

Then you can clone the list like so:

var clone = BioInfo.Select(item => new BioData(item)).ToList();
Matthew Watson
  • 104,400
  • 10
  • 158
  • 276
  • BioData list is in separate file – Supun Silva Nov 12 '20 at 11:43
  • 6
    @SupunSilva And where in your question do you state anything about data being in a file? How do you expect people to be able to answer a question you haven't asked? Please go and edit your question and add all pertinent information! – Matthew Watson Nov 12 '20 at 11:45
  • Employer List has a subList named BioData. I want to deep clone Employer List including all the fields and subLists. – Supun Silva Nov 12 '20 at 11:51
  • @SupunSilva Can you post the declaration of the class that has a sublist named BioData? – Matthew Watson Nov 12 '20 at 12:06
  • With the above code, I can't add new items to the BioData. `new BioData(){IndexID=1, Description ="112dfdf", ....}` – Supun Silva Nov 12 '20 at 13:08
  • @SupunSilva I've added a default constructor to my sample code. As soon as you add a non-default constructor, the auto-generated one is no longer auto-generated, so you have to manually add one. – Matthew Watson Nov 12 '20 at 14:20
  • @QuarK What do you mean? The lining up? Resharper does that for me by default... – Matthew Watson Nov 12 '20 at 16:13
  • @MatthewWatson Yes, it's strange if it's default lining up for Resharper. Mine doesn't add extra spaces after type and name. – QuarK Nov 16 '20 at 10:52
1

You can also use a method or the IClonable Interface to clone your data. I prefere my own method with a clear name (shallow or deep clone):

public sealed class Employer
{
    public int IDNumber { get; set; }
    public DateTime RegDate { get; set; }
    public List<BioData> BioInfo { get; set; }

    public Employer DeepClone()
    {
        Employer loClone = new Employer()
        {
            IDNumber = this.IDNumber,
            RegDate = this.RegDate
        };
        if (this.BioInfo != null)
            loClone.BioInfo = this.BioInfo.Select(item => item.DeepClone()).ToList();
        return loClone;
    }
}

public sealed class BioData 
{
    public int IndexID { get; set; }
    public string Description { get; set; }
    public int Age { get; set; }
    public double Salary { get; set; }

    public BioData DeepClone()
    {
        //Can also use here
        //return this.MemberwiseClone() as BioData;
        return new BioData()
        {
            IndexID = this.IndexID,
            Description = String.Copy(this.Description),
            Age = this.Age,
            Salary = this.Salary
        };
    }
}

UPDATE
To copy the entries from an existing list in the same list, you can use LINQ. (ToList is necessary):

List<Employer> loList = new List<Employer>();
loList.ToList().ForEach(item => loList.Add(item.DeepClone()));
PinBack
  • 2,499
  • 12
  • 16