The optional 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 am I getting this?

+6
source share
3 answers

I am sure that you have not defined your SingleLinkNode class as having a generic parameter type. Thus, an attempt to declare it using one fails.

The error message suggests that SingleLinkNode is a nested class, so I suspect that it might happen that you declare members of SingleLinkNode type T without actually declaring T as a generic parameter for SingleLinkNode . You still need to do this if you want SingleLinkNode be shared, but if not, then you can just use the class as SingleLinkNode , not SingleLinkNode<T> .

An 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 want your nested class to be shared, 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 } 
+2
source

If you want to use IEnumerable<T> , as your post suggests, you need to enable using System.Collections.Generic; .

As for the SingleLinkNode class, I don't know where you got it, this is not part of the .NET environment that I see. I would suggest that it is not implemented using generics, and you would need to add a bunch of castings from object to T

+19
source

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 will need to publish your SingleLinkNode class for more answers ...

John

+2
source

Source: https://habr.com/ru/post/894213/


All Articles