非泛型类型“System.Collections.IEnumerable”不能与类型参数一起使用
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;
}
}
错误:
The non-generic type 'LoLQueue<T>.SingleLinkNode' cannot be used with type arguments
为什么我会得到这个?
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;
}
}
Error:
The non-generic type 'LoLQueue<T>.SingleLinkNode' cannot be used with type arguments
Why do i get this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您想使用
IEnumerable
(如帖子标题所示),则需要包含using System.Collections.Generic;
。至于 SingleLinkNode 类,我不知道你从哪里得到它,它不是我所看到的 .NET 框架的一部分。我猜想它不是使用泛型实现的,并且您需要在各处添加一堆从
object
到T
的转换。If you want to use
IEnumerable<T>
, as your post's title suggests, you need to includeusing System.Collections.Generic;
.As for the SingleLinkNode class, I don't know where you got it, it's not part of the .NET framework that I can see. I'd guess that it isn't implemented using generics, and you'll need to add a bunch of casts from
object
toT
everywhere.我很确定您尚未将
SingleLinkNode
类定义为具有泛型类型参数。因此,尝试用一个人来声明它是失败的。错误消息表明
SingleLinkNode
是一个嵌套类,因此我怀疑可能发生的情况是您正在声明T
类型的SingleLinkNode
成员,而不实际将T
声明为SingleLinkNode
的通用参数。如果您希望SingleLinkNode
是通用的,您仍然需要这样做,但如果不是,那么您可以简单地将该类用作SingleLinkNode
而不是SingleLinkNode;
。我的意思的示例:
如果您确实希望您的嵌套类是通用的,那么这将起作用:
I'm pretty sure you haven't defined your
SingleLinkNode
class as having a generic type parameter. As such, an attempt to declare it with one is failing.The error message suggests that
SingleLinkNode
is a nested class, so I suspect what may be happening is that you are declaring members ofSingleLinkNode
of typeT
, without actually declaringT
as a generic parameter forSingleLinkNode
. You still need to do this if you wantSingleLinkNode
to be generic, but if not, then you can simply use the class asSingleLinkNode
rather thanSingleLinkNode<T>
.Example of what I mean:
If you do want your nested class to be generic, then this will work:
这为我编译:
您需要发布您的 SingleLinkNode 类以获得进一步的答案......
约翰
This compiles for me:
You'll need to post your SingleLinkNode class for further answers...
John