Range Operator in Linq.

Linq is almost providing all the functionalities and i have found one another great operator called range operator which will return a sequence of integer number from start point to number of count. Here is the signature of range operator in Linq.

C#, using GeSHi 1.0.8.8
  1. public static IEnumerable<int> Range(int start, int count)
Parsed in 0.008 seconds at 7.12 KB/s

 

Here the Start means the starting integer of the sequence and count means the number of sequence you want from starting integer. Let’s take a simple example for that where will print 5 to 9 number sequence with the help of range operator. Here is the code for that.

C#, using GeSHi 1.0.8.8
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6.  
  7. namespace ConsoleApplication1
  8. {
  9. class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13. var RangeResult = Enumerable.Range(5, 5);
  14. Console.WriteLine("Range Result");
  15. foreach (int num in RangeResult)
  16. {
  17. Console.WriteLine(num);
  18. }
  19. }
  20.  
  21. }
  22. }
Parsed in 0.013 seconds at 33.41 KB/s

And here is the output for that as expected.

LinqOperators

 

Hope this will help you..

Technorati Tags: ,,
Shout it
Published Wednesday, June 23, 2010 2:02 AM by Jalpesh P. Vadgama

Comments

# re: Range Operator in Linq.

Tuesday, June 22, 2010 5:24 PM by trondaron

Just pointing out, you set out to "simple example for that where will print 5 to 10 number sequence"

But the result of your code produced the numbers 5 to 9 inclusive. Either the result, hypothesis, or the wording for the statement are wrong.

# re: Range Operator in Linq.

Tuesday, June 22, 2010 6:06 PM by Jalpesh P. Vadgama

Thanks Trondaron. I have corrected that.

# re: Range Operator in Linq.

Tuesday, June 22, 2010 11:52 PM by thangchung

I thought this is the same with one of feature's functional programming.

# re: Range Operator in Linq.

Thursday, July 01, 2010 3:19 PM by Chris Eargle

Here are some other ideas for different extension methods:

1) var nums = 1.To(5);

2) var nums = 1.Repeat();

The first gives you a range... and it's more declarative. The second gives you an iterator that infinitely returns T =).

# Object Oriented Programing &raquo; Range Operator in Linq.

Thursday, April 14, 2011 11:28 AM by Object Oriented Programing » Range Operator in Linq.

Pingback from  Object Oriented Programing &raquo; Range Operator in Linq.