DEV Community

Cover image for C# Tip: Should You Use .ToList() or .ToArray()?
Mariem Moalla
Mariem Moalla

Posted on

C# Tip: Should You Use .ToList() or .ToArray()?

It’s not just a matter of taste — your choice should depend on how the collection is consumed.

When working with LINQ in C#, you’ve probably called .ToList() or .ToArray() to materialize a query. But which one should you use?

var items = source.Where(x => x.IsValid).ToList();
// or
var items = source.Where(x => x.IsValid).ToArray();
Enter fullscreen mode Exit fullscreen mode

Simple rule is:

  • Use .ToList() if the consumer will modify the collection (add/remove items).

  • Use .ToArray() if the consumer will only read or mutate existing values without changing the collection size.

For collections around 10,000 items or more , .ToList() is actually slightly faster than .ToArray(), this has been observed in benchmarks!

However, in .NET 9, performance updates optimized .ToArray(), making it more efficient for large datasets.

Recommendation

Don’t just default to one or the other. Think about:

  • Will the data be mutated or only enumerated?

  • Are you on .NET 9+ where .ToArray() is optimized?

Top comments (0)