Enumerable.Range(Int32, Int32) Método

Definição

Gera uma sequência de números inteiros dentro de um intervalo especificado.

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)

Parâmetros

start
Int32

O valor do primeiro inteiro na sequência.

count
Int32

O número de inteiros sequenciais a gerar.

Devoluções

Um IEnumerable<Int32> em C# ou IEnumerable(Of Int32) em Visual Basic que contém um conjunto de números inteiros sequenciais.

Exceções

count é inferior a 0.

-ou-

start + count -1 é maior do que o Int32.MaxValue.

Exemplos

O exemplo de código seguinte demonstra como usar Range para gerar uma sequência de valores.

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

Observações

Este método é implementado através da execução diferida. O valor de retorno imediato é um objeto que armazena toda a informação necessária para realizar a ação. A consulta representada por este método não é executada até que o objeto seja enumerado, seja chamando diretamente o seu método GetEnumerator ou usando foreach em C# ou For Each em Visual Basic.

Aplica-se a