Generates a sequence of integral numbers within a specified range. - CSharp System.Collections.Generic

CSharp examples for System.Collections.Generic:IEnumerable

Description

Generates a sequence of integral numbers within a specified range.

Demo Code

// LICENSE: LGPL 3 - https://www.gnu.org/licenses/lgpl-3.0.txt
using System.Collections.Generic;
using System;/*w w w  .j av  a 2  s.com*/

public class Main{
        /// <summary>
        /// Generates a sequence of integral numbers within a specified range.
        /// </summary>
        /// <param name="start">The value of the first integer in the sequence.</param>
        /// <param name="count">The number of sequential integers to generate.</param>
        /// <returns>The generated sequence.</returns>
        /// <exception cref="ArgumentOutOfRangeException">
        /// <paramref name="count" /> is smaller than 0.
        /// </exception>
        public static IEnumerable<int> Range(int start, int count)
        {
            if (count < 0)
            {
                throw new ArgumentOutOfRangeException("count");
            }

            for (int i = 0; i < count; i++)
            {
                yield return start + i;
            }
        }

        /// <summary>
        /// Generates a sequence of integral numbers within a specified range.
        /// </summary>
        /// <param name="count">The number of sequential integers to generate.</param>
        /// <returns>The generated sequence.</returns>
        /// <remarks>The first value, if not empty, is 0.</remarks>
        /// <exception cref="ArgumentOutOfRangeException">
        /// <paramref name="count" /> is smaller than 0.
        /// </exception>
        public static IEnumerable<int> Range(int count)
        {
            return Range(0, count);
        }
}

Related Tutorials