Reputation: 6766
It is said that IEnumerable is used in a custom collection. One of the uses is the foreach loop used to traverse through the custom collection.
However my question is that instead of making a custom collection that first implements IEnumerable
and then constructing another class to implement IEnumerator
to store a group of custom objects, why can't we just use list<your_customer_object>
.
Thanks in advance for help.
Upvotes: 1
Views: 659
Reputation: 38638
As @ken2k said, you can just List<T>
and you will got a generic version of List with your T type. But if you want to hide this implementation and customize some operations that List<T>
or include new features, you can inherits from the List and implement your own methods. For sample:
public class CustomerCollection : List<Customer> // at this time, you have all methods from List such as Add, Remove, [index] etc...
{
public double AverageAge()
{
return this.Average(customer => customer.Age)
}
// other methods...
}
IEnumeralbe
is the most abstraction of collections in .Net Framework, you can just interate in this abstraction.
Upvotes: 1
Reputation: 10957
I think the source of confusion here is the difference between a "custom collection" and a "collection of custom class instances". Of these two, your List<CustomObject>
is the latter case - you are only reusing a collection created by someone else and taking advantage of generics to keep the item type info, that's all. Truly custom collection would probably implement IEnumerable<T>
, but you will rarely (if ever) need it.
So in this case you not only can, but probably also should use List<CustomObject>
Upvotes: 0
Reputation: 11348
You Can.
public class List<T> : IList<T>, ICollection<T>,
IList, ICollection, IReadOnlyList<T>, IReadOnlyCollection<T>, IEnumerable<T>,
IEnumerable
Upvotes: 0
Reputation: 14751
You would provide your own implementation of IEnumerable<T>
when List<T>
isn't right for your needs. The canonical example of this is when you want to support lazy evaluation of your collection, perhaps with a method that uses yield return
.
Upvotes: 1
Reputation: 225248
If it doesn't do anything else, go ahead. Specialized collections are for collections that need to do... special... things.
(For example, a ListViewItemCollection
would have to notify its parent ListView
when updated, so it needs to implement IList(T)
, and therefore IEnumerable(T)
. If you had a custom collection, it might inherit from that. Inheriting only from IEnumerable(T)
makes sense only when your collection can be enumerated, but not indexed.)
Upvotes: 0
Reputation: 8469
Because you might want to implement a different data structure. IEnumerable
abstracts the concept of sequence, while a List
has the concept of index, adding/removing items and its own, data structure.
As an example of different structure, here's a class which allows you to enter an infinite foreach
loop.
public class InfiniteSequence : IEnumerable<int>
{
public IEnumerator<int> GetEnumerator()
{
return new InfiniteEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
class InfiniteEnumerator : IEnumerator<int>
{
public void Dispose()
{
throw new NotImplementedException();
}
public bool MoveNext()
{
Current++;
return true;
}
public void Reset()
{
Current = 0;
}
public int Current { get; private set; }
object IEnumerator.Current
{
get { return Current; }
}
}
}
Upvotes: 3
Reputation: 46018
Of course you can define your members as List<T>
, that however binds the customer of your class to a specific implementation. If you define the member as IEnumerable<T>
(assuming that the consumers will only want to enumerate your collection) than you can change the implementation to a different collection type that implements the interface without breaking the contract. You if you change it from List<T>
to SortedSet<T>
to implement order your consumers are not affected.
It's basically the advantage of coding against interfaces rather than concrete types.
You can use a different interface, ie. ICollection<T>
if your consumers want more than just enumerate.
Upvotes: 0
Reputation: 6554
You can just use List<T>
or any other of the generic collection types. After all List<T>
implements IEnumerable<T>
.
Do you have a particular edge case that you need to cover with a bespoke collection?
Upvotes: 0
Reputation: 49013
Of course you can use List<T>
.
If List<T>
fits your needs, then use it and don't create a new class that inherits from IEnumerable<T>
.
Upvotes: 1