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?
Using the * Operator The * operator can also be used to repeat elements of a list. When we multiply a list with any number using the * operator, it repeats the elements of the given list. Here, we just have to keep in mind that to repeat the elements n times, we will have to multiply the list by (n+1).
Try the following
var l = Enumerable.Repeat('x',5).ToList();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With