I recently run into trouble when trying to AddRange(IEnumerable) to a List. Probably a classic issue, but I do not really get it yet.
I understand that methods expecting a List parameter are not satisfied with a List, because they might try to add a Base to the List, which is obviously impossible.
But if i get this correctly, since IEnumerables themselves cannot be changed, it ought to work in this case.
The code i thought of looks like this:
class Foo
{
}
class Bar : Foo
{
}
class FooCol
{
private List<Foo> m_Foos = new List<Foo> ();
public void AddRange1(IEnumerable<Foo> foos)
{
m_Foos.AddRange (foos); // does work
}
public void AddRange2<T>(IEnumerable<T> foos) where T : Foo
{
m_Foos.AddRange (foos); // does not work
}
}
class Program
{
static void Main(string[] args)
{
FooCol fooCol = new FooCol ();
List<Foo> foos = new List<Foo> ();
List<Bar> bars = new List<Bar> ();
fooCol.AddRange1 (foos); // does work
fooCol.AddRange1 (bars); // does not work
fooCol.AddRange2 (foos); // does work
fooCol.AddRange2 (bars); // does work
}
}
I tried to pass a hint to the compiler in the AddRange2 method, but this just moved to problem around.
Is my way of thinking flawed? Is this a limitation of the language or is it by design?
IIRC, support for this kind of operations was added to Java 1.5, so maybe it will be added to C# at some point in the future, too...?
This is covariance, and will be fixed in C# 4.0 / .NET 4.0. For now, the generic option is the best answer (for IEnumerable<T>
- not IList<T>
etc).
But within the generic method, you have to think in terms of T
. You could also use Cast<T>
or OfType<T>
with LINQ to achieve something similar.
In C# 3.0 you can use the "Cast" extension method. If you import System.Linq and then use this code:
public void AddRange2<T>(IEnumerable<T> foos) where T : Foo
{
m_Foos.AddRange (foos.Cast<Foo>());
}
Then it should work for you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With