I have an object class TimeDuration
. When I tried to change the value of object taken from the array Span, the value inside the array also changes. How can I get copy of an object that won't change its parent object value? I need to pass this copy to another function where I have to make changes to this object
public void test()
{
List<TimeDuration> Span = new List<TimeDuration>();
TimeDuration ob = new TimeDuration();
ob.FromTime = DateTime.Now;
ob.ToTime = DateTime.Now.AddDays(1);
Span.Add(ob);
//Trying to assign value here!
TimeDuration ob2= Span[1];
ob2.FromTime = DateTime.Now.AddDays(3);
}
public class TimeDuration
{
public DateTime FromTime { get; set; }
public DateTime ToTime { get; set; }
}