Back to: LINQ Tutorial For Beginners and Professionals
LINQ Repeat Method in C# with Examples
In this article, I am going to discuss the LINQ Repeat Method in C# with Examples. Please read our previous article where we discussed the LINQ Range Method in C# with Examples. The Repeat method belongs to the Generation Operator category. Generation Operators allow us to create some specific type of Array, Sequence, or Collection using a single expression instead of creating them manually and populating them using some kind of loop.
LINQ Repeat Method in C#:
The LINQ Repeat Method in C# is used to generate a sequence or a collection with a specified number of elements and each element contains the same value. The following is the signature of the LINQ Repeat Method.
The Repeat method tales 2 integer parameters. The first parameter (i.e. TResult element) specifies the value to be repeated. The second parameter (i.e. int count) specifies the number of times to repeat the value. The return type of this method is IEnumerable<TResult> which is going to contain the repeated values. Here TResult specifies the data type of the value that is going to be repeated in the result sequence. When the count value is less than 0, then it will through ArgumentOutOfRangeException.
Example to Understand LINQ Repeat Method in C#:
Let us see an example to understand the LINQ Repeat Method in C#. The following example shows how to use the Repeat method to generate a sequence of repeated values. In the below example, we are repeating the string “Welcome to DOT NET Tutorials” 10 times using the Repeat method. As the data type of the value is a string, it is going to return IEnumerable<string>. Here, TResult is a string. There is no operator call repeat available in LINQ to write the Query Syntax. So, it only supports the Method Syntax.
using System.Linq; using System; using System.Collections.Generic; namespace LINQRepeatMethodDemo { class Program { static void Main(string[] args) { //Repeating the string value Welcome to DOT NET Tutorials for 10 Times //Using the Repeat Method IEnumerable<string> repeatStrings = Enumerable.Repeat("Welcome to DOT NET Tutorials", 10); //Accessing the collection or sequence using a foreach loop foreach (string str in repeatStrings) { Console.WriteLine(str); } Console.ReadKey(); } } }
Output:
What happens if we pass the count as a Negative Number to the Repeat Method in C#?
If we pass the count value as a Negative Number to the Repeat Method, then it will throw ArgumentOutOfRangeException. For a better understanding, please have a look at the following example. Here, we are passing the count value as -5.
using System.Linq; using System; using System.Collections.Generic; namespace LINQRepeatMethodDemo { class Program { static void Main(string[] args) { //Repeating the string value Welcome to DOT NET Tutorials for 10 Times //Using the Repeat Method IEnumerable<string> repeatStrings = Enumerable.Repeat("Welcome to DOT NET Tutorials", -5); //Accessing the collection or sequence using a foreach loop foreach (string str in repeatStrings) { Console.WriteLine(str); } Console.ReadKey(); } } }
With the above changes in place, now, run the application and you will get the ArgumentOutOfRangeException as shown in the below image.
Note: The Repeat method is implemented using the Deferred Execution. So, the immediate return value is an object which stores all the required information to perform an action. The query represented by this method is not executed until the object is enumerated either by calling its GetEnumerator method directly or by using a for each loop.
In the next article, I am going to discuss the LINQ Empty Method in C# with Examples. Here, in this article, I try to explain the LINQ Repeat Method in C# with an Example.