I think I understand why IEnumerable<T>
inherit from IEnumerable
, after reading the post:
Why does IEnumerable<T>
inherit from IEnumerable?
However, I am not sure how best to implement the non-generic method when appling 2 generic interfaces? Here is an example of the code I am writting:
public interface IComponentA { /* ... Interface A Code ... */ }
public interface IComponentB { /* ... Interface B Code ... */ }
public class ComponentModel: IEnumerable<IComponentA>, IEnumerable<IComponentB>
{
public ComponentModel() { }
private List<IComponentA> ListOfComponentA = new List<IComponentA>();
private List<IComponentB> ListOfComponentB = new List<IComponentB>();
// ... Some public methods to add and remove components (for A and B).
IEnumerator<IComponentA> IEnumerable<IComponentA>.GetEnumerator()
{
return ListOfComponentA.GetEnumerator();
}
IEnumerator<IComponentB> IEnumerable<IComponentB>.GetEnumerator()
{
return ListOfComponentB.GetEnumerator();
}
// The fact that IEnumerable<T> inherits from the non-generic IEnumerable
// now means I have to deal with this.
IEnumerator IEnumerable.GetEnumerator()
{
// Throwing a NotImplementedException is probably not a good idea
// so what should I put in here?
throw new NotImplementedException();
}
}
Suggestions of what to put in the non-generic method are welcome please.
IEnumerable<T> is the base interface for collections in the System. Collections. Generic namespace such as List<T>, Dictionary<TKey,TValue>, and Stack<T> and other generic collections such as ObservableCollection<T> and ConcurrentStack<T>.
Generic Namespace. Contains interfaces and classes that define generic collections, which allow users to create strongly typed collections that provide better type safety and performance than non-generic strongly typed collections.
IEnumerable interface is a generic interface which allows looping over generic or non-generic lists. IEnumerable interface also works with linq query expression. IEnumerable interface Returns an enumerator that iterates through the collection.
ICollection inherits from IEnumerable. You therefore have all members from the IEnumerable interface implemented in all classes that implement the ICollection interface.
I probably wouldn't do that, myself. It can be confusing for a user to have the enumerator enumerate over different things depending on the interface reference calling it, and of course the issue of what the generic returns as well.
Instead, you could just expose a read-only-ish version of the lists as an iterator:
public class ComponentModel
{
public ComponentModel() { }
private List<IComponentA> ListOfComponentA = new List<IComponentA>();
private List<IComponentB> ListOfComponentB = new List<IComponentB>();
public IEnumerable<IComponentA> AComponents
{
get { return ListOfComponentA.Skip(0); }
}
public IEnumerable<IComponentB> BComponents
{
get { return ListOfComponentB.Skip(0); }
}
...
}
By using the Skip(0)
you return an iterator, and it prevents them from casting back to List<IComponentA>
and modifying the List
out from under you.
You could also use a ReadOnlyCollection
of course, but those are kinda clunky since they throw when you try to do mutating ops.
So now, you can iterate over either:
foreach(var a in myModel.AComponents)
{
...
}
foreach(var b in myModel.BComponents)
{
...
}
Also, IF A and B component lists always have the same length, you could have an enumerator over a Tuple
of them in .NET 4.0 and using the Linq Zip() method:
public IEnumerable<Tuple<IComponetA, IComponetB>> Components
{
get
{
return ListOfComponentA.Zip(ListOfComponentB, (a,b) => Tuple.Create(a,b));
}
}
The accepted answer is exactly right; you should implement things that have two sequences as having two sequences, not as being two sequences.
The reason given thus far about why it is a bad idea is sufficient, namely, that it is generally less confusing when you choose composition over inheritance. However, I think it is valuable to point out that there is another reason why implementing two different IEnumerable<T>
interfaces is a really bad idea: we added generic covariance to IEnumerable<T>
in C# 4.
Generic covariance means that you can pass a sequence of Turtles to a method that takes a sequence of Animals, because Turtles are Animals. So what then does the runtime do in this case?
class Weird : IEnumerable<Turtle>, IEnumerable<Giraffe> { ... }
...
Weird weird = whatever;
foreach(Animal animal in weird)
{
...
Is animal
a turtle or a giraffe? The runtime has to pick one of them, and it is completely unclear which one is the right one to pick. This is one of the rare situations in which the C# specification does not say what happens; this is implementation-defined behaviour. Avoid, avoid, avoid.
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