How to Accept Any Collection with an IEnumerable<T> Parameter in C#
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
<ReturnType> <Name>(IEnumerable<T> <items>)
{
foreach (T <item> in <items>)
{
<statements>
}
}
| Part | What it does |
|---|---|
IEnumerable<T> | Any sequence of T values that can be looped over: arrays, lists, sets and more. |
T | The element type, such as int or string: IEnumerable<int> for whole numbers. |
foreach | Reads 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
.csfile withdotnet run. This guide uses .NET SDK 10.0.404.
Step 1: Write the Code
Create a file named main.cs in an empty folder:

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
dotnet run main.cs
6
9
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:
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;
}
/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
| Mistake | What happens | Do this instead |
|---|---|---|
Declaring the parameter as int[] or List<int> when the method only loops | Callers with another collection get error CS1503 | Use IEnumerable<int> |
Converting with .ToArray() or .ToList() just to call the method | Works, but copies the data for nothing | Widen the parameter type instead |
Expecting IEnumerable<T> to have Count or an indexer | It only supports looping | Use LINQ's Count(), or ask for IReadOnlyList<T> when you need indexes |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
CS1503: Argument 1: cannot convert from 'System.Collections.Generic.List<int>' to 'int[]' | The parameter type is narrower than needed | Change it to IEnumerable<int> |
numbers[0] doesn't compile | IEnumerable<T> has no indexer | Loop 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
- The IEnumerable interface, on Microsoft Learn.
- Arrays, on Microsoft Learn.
