Tuesday, March 15, 2011

PHP range() function in .NET?

I need to generate a list of sequential numbers. I know Ruby you can do 1..10 or PHP you can do range(1, 10). Anything like that in .Net already, or do I have to write it? Thanks.

From stackoverflow
  • Would this work for you?

    public List<int> Range(int start, int finish)
    {
      List<int> retList = new List<int>();
      for(int i = start; i <= finish; i++)
      {
         retList.Add(i);
      }
      return retList;
    }
    
    Kevin : I actually wrote a method exactly like this before I saw you could use Enumerable.Range. Thanks.
    scottm : Yeah, that's new to me also. I really need to get a 3.0 book, but I think I'm just going to wait until 4.0
  • In C# (with .NET 3.0 or higher) this should do the job:

    IEnumerable<int> myRange = Enumerable.Range(1, 10);
    
  • You can use Enumerable.Range()

0 comments:

Post a Comment