Uređivanje

Dijeli putem


Enumerable.Repeat<TResult>(TResult, Int32) Method

Definition

Generates a sequence that contains one repeated value.

public:
generic <typename TResult>
 static System::Collections::Generic::IEnumerable<TResult> ^ Repeat(TResult element, int count);
public static System.Collections.Generic.IEnumerable<TResult> Repeat<TResult> (TResult element, int count);
static member Repeat : 'Result * int -> seq<'Result>
Public Function Repeat(Of TResult) (element As TResult, count As Integer) As IEnumerable(Of TResult)

Type Parameters

TResult

The type of the value to be repeated in the result sequence.

Parameters

element
TResult

The value to be repeated.

count
Int32

The number of times to repeat the value in the generated sequence.

Returns

IEnumerable<TResult>

An IEnumerable<T> that contains a repeated value.

Exceptions

count is less than 0.

Examples

The following code example demonstrates how to use Repeat to generate a sequence of a repeated value.

IEnumerable<string> strings =
    Enumerable.Repeat("I like programming.", 15);

foreach (String str in strings)
{
    Console.WriteLine(str);
}

/*
 This code produces the following output:

 I like programming.
 I like programming.
 I like programming.
 I like programming.
 I like programming.
 I like programming.
 I like programming.
 I like programming.
 I like programming.
 I like programming.
 I like programming.
 I like programming.
 I like programming.
 I like programming.
 I like programming.
*/
' Repeat the same string to create a sequence.
Dim sentences As IEnumerable(Of String) =
Enumerable.Repeat("I like programming.", 15)

Dim output As New System.Text.StringBuilder
For Each sentence As String In sentences
    output.AppendLine(sentence)
Next

' Display the output.
Console.WriteLine(output.ToString())

' This code produces the following output:
'
' I like programming.
' I like programming.
' I like programming.
' I like programming.
' I like programming.
' I like programming.
' I like programming.
' I like programming.
' I like programming.
' I like programming.
' I like programming.
' I like programming.
' I like programming.
' I like programming.
' I like programming.

Remarks

This method is implemented by using deferred execution. The immediate return value is an object that stores all the information that is required to perform the 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 foreach in C# or For Each in Visual Basic.

Applies to