为什么堆栈数据在分配 C# 之前会被修改
Why would Stack data modified before it gets assigned C#
我当前的项目确实遇到了一个奇怪的问题。我有示例 类,它将保存一些数据作为一个集合 (List<SampleData>
)。并且我使用了另一个堆栈集合 (Stack<SampleData>
) 来记录添加到第一个列表的数据。修改第一个列表的第一个索引数据后,在我不知情的情况下修改了堆栈数据
public class ActionLog
{
private Stack<SampleData> UndoStack;
public ActionLog()
{
UndoStack = new();
}
public void Log(SampleData Data)
{
UndoStack.Push(Data);
foreach (var item in UndoStack)
System.Console.WriteLine($"{item.Name}");
}
}
public class ActivityControl
{
public ActionLog Logger { get; set; } = new ActionLog();
public List<SampleData> Datas { get; set; } = new List<SampleData>();
public void Initiallize(List<SampleData> datas)
{
Datas.AddRange(datas);
Logger.Log(datas[0]);
}
}
internal class Program
{
static ActivityControl contorl = new ActivityControl();
static void Main(string[] args)
{
List<SampleData> list = new List<SampleData>();
SampleData data = new SampleData()
{
Name = "Data 1"
};
SampleData data2 = new SampleData()
{
Name = "Data 2"
};
list.Add(data);
list.Add(data2);
contorl.Initiallize(list);
contorl.Datas[0].Name = "Data 11";
contorl.Logger.Log(new SampleData() { Name = "Fake Data" });
Console.ReadKey();
}
}
以上代码的输出应该是:Data 1 , Fake Data, Data 1 but i am seeing Data 1, Fake Data, Data 11
我遇到的问题与深拷贝和浅拷贝有关。
我在 SampleData
class 中添加了一个 List<SampleData>DeepCopy()
方法,然后在首次分配时调用该方法。
以下代码解决了该问题:
public class SampleData
{
public string Name { get; set; }
public SampleData DeepCopy()
{
var copy = new SampleData();
copy.Name = this.Name;
return copy;
}
}
public class ActionLog
{
private Stack<List<SampleData>> UndoStack;
public ActionLog()
{
UndoStack = new();
}
public void Log(List<SampleData> Data)
{
UndoStack.Push(Data);
foreach (var item in UndoStack)
System.Console.WriteLine($"{item[0].Name}");
}
}
public class ActivityControl
{
public ActionLog Logger { get; set; } = new ActionLog();
public List<SampleData> Datas { get; set; }
public void Initiallize(List<SampleData> datas)
{
Datas = datas;
List<SampleData> datas1 = new List<SampleData>();
foreach (var dt in Datas)
{
datas1.Add(dt.DeepCopy());
}
Logger.Log(datas1);
}
}
internal class Program
{
static ActivityControl contorl = new ActivityControl();
static void Main(string[] args)
{
List<SampleData> list = new List<SampleData>();
SampleData data = new SampleData()
{
Name = "Data 1"
};
SampleData data2 = new SampleData()
{
Name = "Data 2"
};
list.Add(data);
list.Add(data2);
contorl.Initiallize(list);
contorl.Datas[0].Name = "Data 11";
contorl.Logger.Log(contorl.Datas);
Console.ReadKey();
}
}
我当前的项目确实遇到了一个奇怪的问题。我有示例 类,它将保存一些数据作为一个集合 (List<SampleData>
)。并且我使用了另一个堆栈集合 (Stack<SampleData>
) 来记录添加到第一个列表的数据。修改第一个列表的第一个索引数据后,在我不知情的情况下修改了堆栈数据
public class ActionLog
{
private Stack<SampleData> UndoStack;
public ActionLog()
{
UndoStack = new();
}
public void Log(SampleData Data)
{
UndoStack.Push(Data);
foreach (var item in UndoStack)
System.Console.WriteLine($"{item.Name}");
}
}
public class ActivityControl
{
public ActionLog Logger { get; set; } = new ActionLog();
public List<SampleData> Datas { get; set; } = new List<SampleData>();
public void Initiallize(List<SampleData> datas)
{
Datas.AddRange(datas);
Logger.Log(datas[0]);
}
}
internal class Program
{
static ActivityControl contorl = new ActivityControl();
static void Main(string[] args)
{
List<SampleData> list = new List<SampleData>();
SampleData data = new SampleData()
{
Name = "Data 1"
};
SampleData data2 = new SampleData()
{
Name = "Data 2"
};
list.Add(data);
list.Add(data2);
contorl.Initiallize(list);
contorl.Datas[0].Name = "Data 11";
contorl.Logger.Log(new SampleData() { Name = "Fake Data" });
Console.ReadKey();
}
}
以上代码的输出应该是:Data 1 , Fake Data, Data 1 but i am seeing Data 1, Fake Data, Data 11
我遇到的问题与深拷贝和浅拷贝有关。
我在 SampleData
class 中添加了一个 List<SampleData>DeepCopy()
方法,然后在首次分配时调用该方法。
以下代码解决了该问题:
public class SampleData
{
public string Name { get; set; }
public SampleData DeepCopy()
{
var copy = new SampleData();
copy.Name = this.Name;
return copy;
}
}
public class ActionLog
{
private Stack<List<SampleData>> UndoStack;
public ActionLog()
{
UndoStack = new();
}
public void Log(List<SampleData> Data)
{
UndoStack.Push(Data);
foreach (var item in UndoStack)
System.Console.WriteLine($"{item[0].Name}");
}
}
public class ActivityControl
{
public ActionLog Logger { get; set; } = new ActionLog();
public List<SampleData> Datas { get; set; }
public void Initiallize(List<SampleData> datas)
{
Datas = datas;
List<SampleData> datas1 = new List<SampleData>();
foreach (var dt in Datas)
{
datas1.Add(dt.DeepCopy());
}
Logger.Log(datas1);
}
}
internal class Program
{
static ActivityControl contorl = new ActivityControl();
static void Main(string[] args)
{
List<SampleData> list = new List<SampleData>();
SampleData data = new SampleData()
{
Name = "Data 1"
};
SampleData data2 = new SampleData()
{
Name = "Data 2"
};
list.Add(data);
list.Add(data2);
contorl.Initiallize(list);
contorl.Datas[0].Name = "Data 11";
contorl.Logger.Log(contorl.Datas);
Console.ReadKey();
}
}