Shortest way to create a ListT of a repeated element
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
The shortest way to create a List<T> of repeated elements in C# is Enumerable.Repeat(element, count).ToList(). This one-liner from LINQ creates a sequence containing the specified element repeated the given number of times, then materializes it as a List<T>. For value types, each element is an independent copy. For reference types, all entries point to the same object. Alternatives include array initialization with new T[count] (for default values), list comprehension with Enumerable.Range, and manual loops.
Enumerable.Repeat (Recommended)
Enumerable.Repeat is lazy — it does not allocate the full list until .ToList() is called.
Enumerable.Range with Select
When each element needs to be a new instance (important for reference types):
Array Initialization
For default values, array creation is the fastest approach:
Manual Loop
Simple and explicit, useful when you need complex initialization logic:
Collection Expressions (C# 12)
C# 12 introduces collection expressions with spread:
Performance Comparison
| Method | Readability | Performance | Reference Type Safe |
Enumerable.Repeat().ToList() | Best | Good | No (shared reference) |
Enumerable.Range().Select() | Good | Good | Yes (new instances) |
new int[n] + Array.Fill | Moderate | Best | N/A (value types) |
| Manual loop | Moderate | Good | Yes (if using new) |
Common Pitfalls
- Using
Enumerable.Repeatwith reference types: All elements point to the same object. Modifying one element modifies all. UseEnumerable.Range(0, n).Select(_ => new T())to create independent instances. - Not pre-allocating list capacity in loops:
new List<int>()starts with capacity 0 and resizes as elements are added. For known sizes, pass the capacity to the constructor:new List<int>(count). - Confusing
Enumerable.Repeatwithstringconstructor:new string('x', 5)creates"xxxxx"(a string), not a list. For aList<char>, useEnumerable.Repeat('x', 5).ToList(). - Forgetting
.ToList()afterEnumerable.Repeat:Enumerable.Repeatreturns anIEnumerable<T>, not aList<T>. Without.ToList(), you get a lazy sequence that is re-evaluated each time it is enumerated. - Using
Enumerable.Repeat(new int[3], 5)expecting independent arrays: This creates 5 references to the same array. Mutating one array mutates all five. UseEnumerable.Range(0, 5).Select(_ => new int[3])instead.
Summary
Enumerable.Repeat(value, count).ToList()is the shortest and most readable approach- For reference types, use
Enumerable.Range(0, n).Select(_ => new T())to create independent instances - For maximum performance with value types, use
new T[n]withArray.Fill - Pre-allocate
List<T>capacity when the size is known in advance - C# 12 collection expressions support spread:
[.. Enumerable.Repeat(x, n)]

