I'd like to create a class of the following type
public class EnumerableDisposer<IEnumerable<IDisposable>>
But it won't let me declare it this way. I've also tried:
public class EnumerableDisposer<T> : IDisposable where T : IEnumerable<J> where J : IDisposable
But the compiler tells me that the type/namespace J could not be found.
What is it I have to do to create this class?
you need to definie
J
.eg
better would be:
You can do this by adding an extra
J
type parameter:Note that these
T
andJ
parameters are independent of any parameters in the outer class, even if they have the same names.You need to do:
Unfortunately, in order to wrap any internal type (
IEnumerable<J>
, in your code), your "wrapping" class needs to have the typeJ
defined in the generic definition. In addition, in order to add theIEnumerable<J>
constraint, you need to have the other typeT
.That being said, if you want to avoid the double generic type specification, you could always rework this as follows:
This forces you to construct it with an
IEnumerable<T>
where T is IDisposable, with a single generic type. Since you're effectively adding theIEnumerable<T>
constraint via the constructor, this will function as well as the previous option. The only downside is that you need to have the generic done at construction time, but given the name, I suspect this will be fine...