在 C# 中玩堆栈集合时,我遇到了以下问题。正是我不确定为什么会发生这种情况。请说明原因和解决方案的替代方案。
问题 -
具有 Stack 作为属性的类。例如,将该类命名为 Progress。T 是 Item 类类型。
现在,每当用户取得任何进展时,我们都会将其存储在堆栈中。如果用户在两者之间离开,那么下次我们将从该阶段从堆栈中查看该项目。下面的代码片段将给出正在尝试的内容的想法......
using static System.Console;
using System.Collections.Generic;
using Newtonsoft.Json;
namespace StackCollection
{
class Program
{
static void Main(string[] args)
{
Progress progress = new Progress();
progress.Items.Push(new Item { PlanID = null, PlanName = "Plan A" });
var jsonString = JsonConvert.SerializeObject(progress);
var temp = JsonConvert.DeserializeObject<Progress>(jsonString);
temp.Items.Push(new Item { PlanID = null, PlanName = "Plan B" });
jsonString = JsonConvert.SerializeObject(temp);
temp = JsonConvert.DeserializeObject<Progress>(jsonString);
temp.Items.Push(new Item { PlanID = null, PlanName = …Run Code Online (Sandbox Code Playgroud)