从堆栈/队列中弹出堆栈

  • 本文关键字:堆栈 队列 c# stack queue
  • 更新时间 :
  • 英文 :


我的问题是如何从队列中获取堆栈。该程序的工作方式应该是生成堆栈(如下所示(,将这些堆栈填充数据(如下所述(,然后卸载并显示其中的数据。现在它只是向我抛出了一个CS1061异常。例如,5就在那里,实际的代码是从数组中挑选一个随机字符串。

public void newCustomers()
{
var customer = new Stack();
store.Enqueue(customer);
}
public void Shop()
{
var customer = store.Dequeue();
customer.Push(5);
//^currently this doesn't work. I'm assuming the typing for customer is wrong.
store.Enqueue(customer);
}

CS1061

严重性代码描述项目文件行禁止显示状态错误CS1061"object"不包含"Push"的定义,并且找不到接受"object"类型的第一个参数的可访问扩展方法"Push"(是否缺少using指令或程序集引用?(

您使用的是非泛型Queue类。Dequeue()方法返回一个必须强制转换为Stack:的object

var customer = (Stack)store.Dequeue();
customer.Push(5);

我建议使用通用队列类Queue<T>

最新更新