Dan*_*npe 9 .net c# generics queue nodes
using System.Collections.Generic;
public sealed class LoLQueue<T> where T: class
{
private SingleLinkNode<T> mHe;
private SingleLinkNode<T> mTa;
public LoLQueue()
{
this.mHe = new SingleLinkNode<T>();
this.mTa = this.mHe;
}
}
Run Code Online (Sandbox Code Playgroud)
错误:
The non-generic type 'LoLQueue<T>.SingleLinkNode' cannot be used with type arguments
Run Code Online (Sandbox Code Playgroud)
为什么我会这样?
Dav*_*Yaw 24
如果您想使用IEnumerable<T>,如您的帖子标题所示,您需要包括using System.Collections.Generic;.
至于SingleLinkNode类,我不知道你从哪里得到它,它不是我能看到的.NET框架的一部分.我猜它不是使用泛型实现的,你需要object在T任何地方添加一堆强制转换.
我很确定您还没有将您的SingleLinkNode类定义为具有泛型类型参数。因此,尝试用一个人来声明它是失败的。
错误消息表明这SingleLinkNode是一个嵌套类,所以我怀疑可能发生的情况是您正在声明SingleLinkNodetype的成员T,而没有实际声明T为 的泛型参数SingleLinkNode。如果您想要通用,您仍然需要这样做SingleLinkNode,但如果不是,那么您可以简单地使用类 as 而SingleLinkNode不是SingleLinkNode<T>。
我的意思的例子:
public class Generic<T> where T : class
{
private class Node
{
public T data; // T will be of the type use to construct Generic<T>
}
private Node myNode; // No need for Node<T>
}
Run Code Online (Sandbox Code Playgroud)
如果您确实希望嵌套类是通用的,那么这将起作用:
public class Generic<T> where T : class
{
private class Node<U>
{
public U data; // U can be anything
}
private Node<T> myNode; // U will be of type T
}
Run Code Online (Sandbox Code Playgroud)