Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IEnumerable property without type

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?

like image 948
Guilherme Avatar asked Jul 17 '13 15:07

Guilherme


People also ask

What is IEnumerable<> in c#?

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.

What is t in IEnumerable?

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>.

Why do we need IEnumerable in c#?

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.

Is IEnumerable a collection?

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.


1 Answers

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).

like image 129
SLaks Avatar answered Nov 10 '22 03:11

SLaks