Skip to main content

How to Accept Any Collection with an IEnumerable<T> Parameter in C#

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

A method that only loops over its input doesn't care whether it gets an array, a List or a HashSet. Declare the parameter as IEnumerable<T>, the interface all of them share, and callers can pass whichever collection they have.

Syntax​

Syntax
<ReturnType> <Name>(IEnumerable<T> <items>)
{
foreach (T <item> in <items>)
{
<statements>
}
}
PartWhat it does
IEnumerable<T>Any sequence of T values that can be looped over: arrays, lists, sets and more.
TThe element type, such as int or string: IEnumerable<int> for whole numbers.
foreachReads the items one by one, which is all IEnumerable<T> promises.

Microsoft's IEnumerable reference: "Exposes the enumerator, which supports a simple iteration over a collection of a specified type." And: "IEnumerable<T> is the base interface for collections in the System.Collections.Generic namespace such as List<T>, Dictionary<TKey,TValue>, and Stack<T>". Arrays qualify too; the arrays reference: "Single-dimensional arrays also implement IList<T> and IEnumerable<T>."

The Example​

A Sum method adds up whole numbers. The program calls it three times with three different collections: an array, a List and a HashSet.

Prerequisites​

  • The .NET 10 SDK or newer, which runs a single .cs file with dotnet run. This guide uses .NET SDK 10.0.404.

Step 1: Write the Code​

Create a file named main.cs in an empty folder:

main.cs, which passes an int array of 1, 2 and 3, a List of 4 and 5, and a HashSet of 6 to a static Sum method whose parameter is an IEnumerable of int, and which adds the numbers up with a foreach loop.

main.cs
int[] array = [1, 2, 3];
List<int> list = [4, 5];
HashSet<int> set = [6];

Console.WriteLine(Sum(array));
Console.WriteLine(Sum(list));
Console.WriteLine(Sum(set));

static int Sum(IEnumerable<int> numbers)
{
int total = 0;
foreach (int n in numbers)
{
total += n;
}

return total;
}

Step 2: Run It​

Terminal
dotnet run main.cs
Output
6
9
6

A zsh terminal where dotnet run main.cs prints 6, 9 and 6.

One method handled all three collections: 1 + 2 + 3 = 6, 4 + 5 = 9, and the set's single 6. The command is the same in zsh, bash and PowerShell.

What Happens with a Narrower Parameter Type​

Declare the parameter as int[], and a List<int> no longer fits:

main.cs
List<int> list = [4, 5];
Console.WriteLine(Sum(list));

static int Sum(int[] numbers)
{
int total = 0;
foreach (int n in numbers)
{
total += n;
}

return total;
}
Output
/Users/you/demo/main.cs(2,23): error CS1503: Argument 1: cannot convert from 'System.Collections.Generic.List<int>' to 'int[]'

The build failed. Fix the build errors and run again.

The method only loops, but its signature demanded an array. IEnumerable<int> asks for exactly what the method needs.

Common Mistakes​

MistakeWhat happensDo this instead
Declaring the parameter as int[] or List<int> when the method only loopsCallers with another collection get error CS1503Use IEnumerable<int>
Converting with .ToArray() or .ToList() just to call the methodWorks, but copies the data for nothingWiden the parameter type instead
Expecting IEnumerable<T> to have Count or an indexerIt only supports loopingUse LINQ's Count(), or ask for IReadOnlyList<T> when you need indexes

Troubleshooting​

SymptomLikely causeFix
CS1503: Argument 1: cannot convert from 'System.Collections.Generic.List<int>' to 'int[]'The parameter type is narrower than neededChange it to IEnumerable<int>
numbers[0] doesn't compileIEnumerable<T> has no indexerLoop with foreach, or ask for a list type

FAQ​

Can I pass a LINQ query or a string?​

Yes, anything that implements IEnumerable<T>. A LINQ query over numbers is an IEnumerable<int>, and a string is an IEnumerable<char>, so a method that takes IEnumerable<char> accepts a string.

Why not always ask for a List?​

A parameter should ask for the least it needs. IEnumerable<T> accepts arrays, lists, sets and queries; List<T> accepts only lists.

Conclusion​

When a method only loops over its input, declare the parameter as IEnumerable<T>. Arrays, lists and sets all implement it, so one method serves every caller, and none of them has to convert anything first.

Additional Resources​