Enumerable.Repeat<TResult>(TResult, Int32) メソッド

定義

繰り返される 1 つの値を含むシーケンスを生成します。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)

型パラメーター

TResult

結果のシーケンスで繰り返される値の型。The type of the value to be repeated in the result sequence.

パラメーター

element
TResult

繰り返される値。The value to be repeated.

count
Int32

生成されたシーケンスで値を繰り返す回数。The number of times to repeat the value in the generated sequence.

戻り値

IEnumerable<TResult>

繰り返される値を含む IEnumerable<T>An IEnumerable<T> that contains a repeated value.

例外

count が 0 未満です。count is less than 0.

次のコード例では Repeat を使用して、繰り返される値のシーケンスを生成する方法を示します。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.
MsgBox(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.

注釈

このメソッドは、遅延実行を使用して実装されます。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. か、呼び出すことによって、オブジェクトが列挙されるまで、このメソッドによって表されるクエリは実行されません、GetEnumeratorメソッドを使用して直接またはforeachVisual C# またはFor EachVisual Basic で。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 Visual C# or For Each in Visual Basic.

適用対象