Enumerable.Range(Int32, Int32) メソッド

定義

指定した範囲内の整数のシーケンスを生成します。

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

シーケンス内の最初の整数の値。

count
Int32

生成する連続した整数の数。

戻り値

IEnumerable<Int32>

連続した整数の範囲を含む IEnumerable<Int32> (C# の場合) または IEnumerable(Of Int32) (Visual Basic の場合)。

例外

count が 0 未満です。

または

start + count -1 が Int32.MaxValue より大きい。

次のコード例は、値のシーケンスを生成するために使用 Range する方法を示しています。

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

注釈

このメソッドは、遅延実行を使用して実装されます。 即時戻り値は、アクションの実行に必要なすべての情報を格納するオブジェクトです。 か、呼び出すことによって、オブジェクトが列挙されるまで、このメソッドによって表されるクエリは実行されません、GetEnumeratorメソッドを使用して直接またはforeachVisual C# またはFor EachVisual Basic で。

適用対象