0

Here is my Class:

public class Information
 {
     public Tuple<string, string> pair { get; set; }
     public double Signal { get; set; }
     public double SigmaMove { get; set; }
     public DateTime Date { get; set; }
 }

 Inforamtion A = new Information() {};
  1. I want to use A.Clone() but there is no such method?

  2. How to Copy a List<Information> A = B by value not by reference(Change of A did not influent B, previous solutions in the website do not work .... I tried)?

    var TodayInformation = YesterdayInformation.Select(o => new Information
            {
                pair = o.pair,
                Signal = o.Signal,
                SigmaMove = o.SigmaMove,
                Date = o.Date,
    
            }).ToList();
    

    If I copy this way, and suppose pair is also a complicate new Class(e.g List<NewClass>), so does pair = o.pair, need be modified to guarantee TodayInformation is passed by value?

  3. When I add a Initializing Constructor in the Class Information

     public class Information
     {
       public Tuple<string, string> pair { get; set; }
       public double Signal { get; set; }
       public double SigmaMove { get; set; }
       public DateTime Date { get; set; }
    
       public Information(Information Tem)
       {
        pair = Tem.pair;
        Signal  = Tem.Signal ;
        SigmaMove = Tem.SigmaMove;
        Date = Tem.Date;
       }
    }
    

Then Inforamtion A = new Information(){}; this constructor no longer work, is it becasue of covered by the new Constructor? Then how to remain this constructor?

user6703592
  • 1,004
  • 1
  • 12
  • 27

4 Answers4

2

You could overload the constructor, that way the empty one can still be used as well as one that takes in an object to copy eg.

public class Information
{
    public Tuple<string, string> pair { get; set; }
    public double Signal { get; set; }
    public double SigmaMove { get; set; }
    public DateTime Date { get; set; }

    public Information()
    {}

    public Information(Information Tem)
    {
        pair = Tem.pair;
        Signal  = Tem.Signal ;
        SigmaMove = Tem.SigmaMove;
        Date = Tem.Date;
    }
}
Kirlac
  • 684
  • 6
  • 18
2
  1. An object can be cloned implementing IClonable interface

    public class Information : ICloneable
    {
       public Tuple<string, string> pair { get; set; }
       public double Signal { get; set; }
       public double SigmaMove { get; set; }
       public DateTime Date { get; set; }
    
       public Information(Information Tem)
       {
          pair = Tem.pair;
          Signal  = Tem.Signal ;
          SigmaMove = Tem.SigmaMove;
          Date = Tem.Date;
       }           
    
       #region ICloneable Members
       public Information Clone() { return new Information(this); }
       object ICloneable.Clone()
       {
          return Clone();
       }
       #endregion
    }
    
  2. You can copy a list by value. Follow any of the steps below If you want to know more about extension method follow this link

You can use extension method as following

    static class Extensions
    {
      public static IList<T> Clone<T>(this IList<T> listToClone) where T: ICloneable
      {
         return listToClone.Select(item => (T)item.Clone()).ToList();
      }
    }

OR If you do not want to use IClonable interface

    var InformationList = new List<Information>(infoList);

EDIT

If there is a nested complex class, make sure you have also implemented IClonable interface for that class too and clone it.

  1. You can overload a constructor (as Kirlac mentioned)

    public class Information
    {
      public Tuple<string, string> pair { get; set; }
      public double Signal { get; set; }
      public double SigmaMove { get; set; }
      public DateTime Date { get; set; }
    
      public Information()
      {}
    
      public Information(Information Tem)
      {
        pair = Tem.pair;
        Signal  = Tem.Signal ;
        SigmaMove = Tem.SigmaMove;
        Date = Tem.Date;
      }
    }
    
PaulShovan
  • 2,140
  • 1
  • 13
  • 22
0

Your class information needs to implement iClonable Go have a look at a few answers in How to Clone Objects

Community
  • 1
  • 1
Neil
  • 641
  • 1
  • 7
  • 21
0

Not a very elegant method, in fact crude, but works .... I serialize the list into a json string and deserialize it back into the new required list. The reference between the two is no longer there and each list can be manipulated independently of the other.

anil
  • 598
  • 1
  • 6
  • 20