Enumerable.Range(Int32, Int32) Enumerable.Range(Int32, Int32) Enumerable.Range(Int32, Int32) Enumerable.Range(Int32, Int32) Method

定義

產生指定範圍內的整數序列。Generates a sequence of integral numbers within a specified range.

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)

參數

start
Int32 Int32 Int32 Int32

序列中第一個整數的值。The value of the first integer in the sequence.

count
Int32 Int32 Int32 Int32

要產生的循序整數數目。The number of sequential integers to generate.

傳回

包含一個循序整數範圍之 C# 的 IEnumerable<Int32> 或 Visual Basic 的 IEnumerable(Of Int32)An IEnumerable<Int32> in C# or IEnumerable(Of Int32) in Visual Basic that contains a range of sequential integral numbers.

例外狀況

count 小於 0。count is less than 0.

-或--or- start + count -1 大於 MaxValuestart + count -1 is larger than MaxValue.

範例

下列程式碼範例示範如何使用Range產生一連串的值。The following code example demonstrates how to use Range to generate a sequence of values.

// 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.
MsgBox(output.ToString())

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

備註

實作這個方法是使用延後的執行。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.

適用於