I'm trying to make a property like the official DataGrid.ItemsSource, from MSDN:
public IEnumerable ItemsSource { get; set; }
This provides the support of any type, in any derived class. With this, I can set something like
var list = new List<ObservableCollection<KeyValuePair<decimal, bool>>>();
MyDataGrid.ItemsSource = list;
But when I try to make a property of an IEnumerable without the Type T, exactly as MSDN says, I get an error on VisualStudio:
Using the generic type 'System.Collections.Generic.IEnumerable<T>' requires 1 type arguments
So, what is wrong?
IEnumerable is an interface defining a single method GetEnumerator() that returns an IEnumerator interface. It is the base interface for all non-generic collections that can be enumerated. This works for read-only access to a collection that implements that IEnumerable can be used with a foreach statement.
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>.
IEnumerable is best to query data from in-memory collections like List, Array etc. IEnumerable doesn't support add or remove items from the list. Using IEnumerable we can find out the no of elements in the collection after iterating the collection. IEnumerable supports deferred execution.
IEnumerable<T> is an interface that represents a sequence. Now; collections can usually be used as sequences (so... List<T> implements IEnumerable<T> ), but the reverse is not necessarily true. In fact, it isn't strictly required that you can even iterate a sequence ( IEnumerable<T> ) more than once.
You need to use the non-generic type System.Collections.IEnumerable
.
(note the different namespace)
Note that in .Net 4.0+, you can use IEnumerable<object>
instead (due to covariance).
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