我的问题是关于如何从队列中取出堆栈。该程序应通过生成堆栈(如下所示),将这些堆栈填充数据(也如下所示),然后卸载并在其中显示数据来工作。现在,它只是向我抛出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'对象'不包含'推'的定义,并且找不到可以接受的扩展方法'推'接受类型为'对象'的第一个参数(您是否缺少using指令或程序集引用?)
答案 0 :(得分:1)
您正在使用非泛型Queue
类。 Dequeue()
方法返回一个object
,您必须将其强制转换为Stack
:
var customer = (Stack)store.Dequeue();
customer.Push(5);
我建议改为使用通用队列类Queue<T>
。