“Until” extension method for integers in C#

I wrote this simple snippet today, and thought I’d share:

public static IEnumerable<int> Until(this int from, int to)
{
    for (int i = from; i <= to; i++)
    {
        yield return i;
    }
}

Its purpose is to return number ranges:

IEnumerable<int> firstTen = 1.Until(10);
foreach (int i in firstTen)
{
    Console.Write(i + " ");
}

Which outputs the following:

1 2 3 4 5 6 7 8 9 10


This entry was posted in C# and tagged , . Bookmark the permalink.
  • Delicious
  • Facebook
  • Reddit
  • StumbleUpon
  • Twitter
  • RSS Feed
  • Google
  • Digg

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>