Enumerable.Repeat<TResult>(TResult, Int32) Метод
Определение
Генерирует последовательность, содержащую одно повторяющееся значение.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.
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.
Комментарии
Этот метод реализуется с помощью отложенного выполнения.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
метода напрямую или с foreach
помощью в Visual C# или в For Each
Visual 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.