Enumerable.Range(Int32, Int32) Metodo

Definizione

Genera una sequenza di numeri integrali all'interno di un intervallo specificato.

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)

Parametri

start
Int32

Valore del primo intero nella sequenza.

count
Int32

Numero di interi sequenziali da generare.

Valori restituiti

Un IEnumerable<Int32> in C# o IEnumerable(Of Int32) in Visual Basic che contiene un intervallo di numeri integrali sequenziali.

Eccezioni

count è minore di 0.

oppure

start + count -1 è maggiore di Int32.MaxValue.

Esempio

Nell'esempio di codice seguente viene illustrato come usare Range per generare una sequenza di valori.

// 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

Commenti

Questo metodo viene implementato usando l'esecuzione posticipata. Il valore restituito immediato è un oggetto che archivia tutte le informazioni necessarie per eseguire l'azione. La query rappresentata da questo metodo non viene eseguita finché l'oggetto non viene enumerato chiamando direttamente il relativo metodo GetEnumerator o usando foreach in C# o For Each in Visual Basic.

Si applica a