Shortest way to create a List<T> of a repeated element

xyz picture xyz · Jul 13, 2009 · Viewed 14.3k times · Source

With the String class, you can do:

string text = new string('x', 5);
//text is "xxxxx"

What's the shortest way to create a List< T > that is full of n elements which are all the same reference?

Answer

JaredPar picture JaredPar · Jul 13, 2009

Try the following

var l = Enumerable.Repeat('x',5).ToList();