Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The non-generic type 'System.Collections.IEnumerable' 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?

like image 471
Danpe Avatar asked Aug 03 '11 18:08

Danpe


3 Answers

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.

like image 134
David Yaw Avatar answered Sep 28 '22 10:09

David Yaw


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
}
like image 35
dlev Avatar answered Sep 28 '22 10:09

dlev


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

like image 23
JohnD Avatar answered Sep 28 '22 11:09

JohnD