I need to pass an IEnumerable
and a delegate to my method, and then filter it and return an IEnumerable
collection with numbers that pass filter.
Func<int, bool> filter = x => x % 3 == 0;
public static IEnumerable<int> Filter(IEnumerable<int> numbers, Func<int, bool> filter)
{
foreach (int number in numbers)
{
if (filter.Invoke(number))
{
// add number to collection
}
}
// return collection
}
I have tried creating a new collection like this:
IEnumerable<int> result = new IEnumerable<int>();
But it is not possible since IEnumerable
is an abstract class.
How should I create and return this collection? I know that there is a lot of easy ways to do this, but can't find any.