From the course: .NET Essentials: Working with LINQ

Unlock this course with a free trial

Join today to access over 22,600 courses taught by industry experts.

The Repeat method

The Repeat method

- Have you ever had this requirement in code you have a list or an array and it needs to be populated with data, but it's not supposed to be populated with the default values like say, for an integer of zero or for a string with no. I need to have another value in there but it needs to be the same value for the entire array or list. For my example, i need a list of integers that has 120 number threes in it. So you might write code like this. Declare your variable here, write a for loop from zero to 120, and then add these items. And this works if I run this code, i get 120 threes in my list, but this is using statement based code and we try not to do that when we are doing Functional programming. So instead the preferred way of doing this, is to use a Enumerable dot Repeat. Tell it what the data type is, what number you want and how many. Let me comment out this line of code, so we don't have to look at that…

Contents