C# Enumerable Range

Description

Enumerable Range Generates a sequence of integral numbers within a specified range.

Syntax

Enumerable.Range has the following syntax.


public static IEnumerable<int> Range(
  int start,
  int count
)

Parameters

Enumerable.Range has the following parameters.

  • start - The value of the first integer in the sequence.
  • count - The number of sequential integers to generate.

Returns

Enumerable.Range method returns <

Example

The following code example demonstrates how to use Range to generate a sequence of values.


//from ww  w  . j  a  v a 2s  .c  o m
using System;
using System.Linq;
using System.Collections.Generic;
public class MainClass{
  public static void Main(String[] argv){  

   IEnumerable<int> squares = Enumerable.Range(1, 10).Select(x => x * x);

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


  }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System.Linq »




Enumerable