How to insert value in IEnumerable <int>?

I have it:

IEnumerable<int> intYear = Enumerable.Empty<int>(); 

how can i insert some values? I do not see any intYear.Add () method.

Like 2010, 2011, etc.

+6
source share
4 answers

To do this, use IList<int> instead of IEnumerable<int> :

 IList<int> intYear = new List<int>(); intYear.Add(2011); // and so on 

IList<T> implements IEnumerable<T> , so you can pass it to any method that takes an IEnumerable<T> argument.

+9
source

You need to use ICollection<T> , since IEnumerable<T> is only used to iterate over collections.

 ICollection<int> years = new List<T>(); years.Add(2010); years.Add(2011); 
+6
source

IEnumerable has no add method. Instead, you should use IList.

 IList<int> intYear = new List<int>(); intYear.Add(2010); 
+3
source

Using the Concat (IEnumerable) method of the IEnumerable class, you can first populate all the values โ€‹โ€‹in the list and then assign it to the IEnumerable list.

0
source

Source: https://habr.com/ru/post/903098/


All Articles