ExtensionMethod.NET Home of 820 C#, Visual Basic, F# and Javascript extension methods

AsSequenceTo

Creates a numeric list of integers starting at the current instance and ending at the maximum value.

Source

using System;
using System.Collections.Generic;
using System.Linq;

namespace IntegerExtensions
{
    /// <summary>
    /// Contains extension methods for Lists of integers
    /// </summary>
    public static class IntegerExtensions
    {

        /// <summary>
        /// Creates a numeric list of integers starting at the current instance and ending 
        /// at the maximum value.
        /// </summary>
        /// <param name="instance">The instance.</param>
        /// <param name="maxValue">The initial value.</param>
        /// <returns></returns>
        public static List<int> AsSequenceTo(this int instance, int maxValue)
        {
            // Validate arguments
            if (maxValue < instance) throw new ArgumentOutOfRangeException("maxValue", maxValue, "maxValue must not be less than the instance. ");

            int count = (maxValue - instance + 1);
            return Enumerable.Range(instance, count).ToList();
        }
    }
}

Example

[TestMethod]
public void AsSequenceTo_EndsAtMximumValue()
{
    // ARRANGE
    const int initialValue = 3;
    const int maximumValue = 5;
    const int expectedEnd = maximumValue;

    // ACT
    var result = initialValue.AsSequenceTo(maximumValue);
    var actual = result.Last();

    // ASSERT
    Assert.AreEqual(expectedEnd, actual);
}

Author: Duane Wingett

Submitted on: 25 feb 2016

Language: C#

Type: System.Int32

Views: 1715