非泛型类型“System.Collections.IEnumerable”不能与类型参数一起使用

发布于 2024-11-28 00:49:49 字数 460 浏览 0 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

極樂鬼 2024-12-05 00:49:49

如果您想使用 IEnumerable(如帖子标题所示),则需要包含 using System.Collections.Generic;

至于 SingleLinkNode 类,我不知道你从哪里得到它,它不是我所看到的 .NET 框架的一部分。我猜想它不是使用泛型实现的,并且您需要在各处添加一堆从 objectT 的转换。

If you want to use IEnumerable<T>, as your post's title suggests, you need to include using 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 to T everywhere.

一场信仰旅途 2024-12-05 00:49:49

我很确定您尚未将 SingleLinkNode 类定义为具有泛型类型参数。因此,尝试用一个人来声明它是失败的。

错误消息表明 SingleLinkNode 是一个嵌套类,因此我怀疑可能发生的情况是您正在声明 T 类型的 SingleLinkNode 成员,而不实际将 T 声明为 SingleLinkNode 的通用参数。如果您希望 SingleLinkNode 是通用的,您仍然需要这样做,但如果不是,那么您可以简单地将该类用作 SingleLinkNode 而不是 SingleLinkNode;

我的意思的示例:

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>
}

如果您确实希望您的嵌套类是通用的,那么这将起作用:

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
}

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 of SingleLinkNode of type T, without actually declaring T as a generic parameter for SingleLinkNode. You still need to do this if you want SingleLinkNode to be generic, but if not, then you can simply use the class as SingleLinkNode rather than SingleLinkNode<T>.

Example of what I mean:

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>
}

If you do want your nested class to be generic, then this will work:

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
}
溺深海 2024-12-05 00:49:49

这为我编译:

public sealed class SingleLinkNode<T>
{

}

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;
    }
}

您需要发布您的 SingleLinkNode 类以获得进一步的答案......

约翰

This compiles for me:

public sealed class SingleLinkNode<T>
{

}

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;
    }
}

You'll need to post your SingleLinkNode class for further answers...

John

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文