Enumerable.Range(Int32, Int32) Metoda

Definicja

Generuje sekwencję liczb całkowitych w określonym zakresie.

public:
 static System::Collections::Generic::IEnumerable<int> ^ Range(int start, int count);
public static System.Collections.Generic.IEnumerable<int> Range(int start, int count);
static member Range : int * int -> seq<int>
Public Function Range (start As Integer, count As Integer) As IEnumerable(Of Integer)

Parametry

start
Int32

Wartość pierwszej liczby całkowitej w sekwencji.

count
Int32

Liczba sekwencyjnych liczb całkowitych do wygenerowania.

Zwraca

IEnumerable<Int32> w języku C# lub IEnumerable(Of Int32) w Visual Basic, który zawiera zakres sekwencyjnych liczb całkowitych.

Wyjątki

count wartość jest mniejsza niż 0.

— lub —

start + count -1 jest większy niż Int32.MaxValue.

Przykłady

W poniższym przykładzie kodu pokazano, jak używać Range metody do generowania sekwencji wartości.

// Generate a sequence of integers from 1 to 10
// and then select their squares.
IEnumerable<int> squares = Enumerable.Range(1, 10).Select(x => x * x);

foreach (int num in squares)
{
    Console.WriteLine(num);
}

/*
 This code produces the following output:

 1
 4
 9
 16
 25
 36
 49
 64
 81
 100
*/
' Generate a sequence of integers from 1 to 10
' and project their squares.
Dim squares As IEnumerable(Of Integer) =
Enumerable.Range(1, 10).Select(Function(x) x * x)

Dim output As New System.Text.StringBuilder
For Each num As Integer In squares
    output.AppendLine(num)
Next

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

' This code produces the following output:
'
' 1
' 4
' 9
' 16
' 25
' 36
' 49
' 64
' 81
' 100

Uwagi

Ta metoda jest implementowana przy użyciu odroczonego wykonania. Natychmiastowa wartość zwracana to obiekt, który przechowuje wszystkie informacje wymagane do wykonania akcji. Zapytanie reprezentowane przez tę metodę nie jest wykonywane, dopóki obiekt nie zostanie wyliczony przez wywołanie metody GetEnumerator bezpośrednio lub przy użyciu foreach w języku C# lub For Each w Visual Basic.

Dotyczy