M
Michi Henning
I can pass a generic collection as ICollection<T> just fine:
static void flatCollection(ICollection<int> c) {}
// ...
List<int> l = new List<int>();
flatCollection(l); // Works fine
Now I want to pass nested collections generically:
static void nestedCollection(ICollection<ICollection<int>> c) {}
List<List<int>> l = new List<List<int>>();
nestedCollection(l); // Compile-time error
The compiler complains with:
Error 2 Argument '1': cannot convert from
'System.Collections.Generic.List<System.Collections.Generic.List<int>>'
to
'System.Collections.Generic.ICollection<System.Collections.Generic.ICollection<int>>'
This seems exceedingly strange. If I change the signature of
nestedCollection to:
static void nestedCollection(ICollection<List<int>> c) {}
things work. But that defeats the purpose because now, I can no longer
generically pass collections of collections. What gives?
Cheers,
Michi.
static void flatCollection(ICollection<int> c) {}
// ...
List<int> l = new List<int>();
flatCollection(l); // Works fine
Now I want to pass nested collections generically:
static void nestedCollection(ICollection<ICollection<int>> c) {}
List<List<int>> l = new List<List<int>>();
nestedCollection(l); // Compile-time error
The compiler complains with:
Error 2 Argument '1': cannot convert from
'System.Collections.Generic.List<System.Collections.Generic.List<int>>'
to
'System.Collections.Generic.ICollection<System.Collections.Generic.ICollection<int>>'
This seems exceedingly strange. If I change the signature of
nestedCollection to:
static void nestedCollection(ICollection<List<int>> c) {}
things work. But that defeats the purpose because now, I can no longer
generically pass collections of collections. What gives?
Cheers,
Michi.