+92 332 4229 857 99ProjectIdeas@Gmail.com

Enumerable Range() Method (C#.net)



The following code shows how to generate a sequence of numbers and then applying query on it. See the code below.






In this first code snippet, generating a sequence from 1 to 10 and selecting each value in array and square it and display.

IEnumerable<int> query =
                Enumerable.Range(1, 10).Select(x => x * x);

         foreach (var item in query)
             Console.Write(item + " ");
 
Output of the program is:
1 4 9 16 25 36 49 64 81 100 

Second code snippet,
 
int[] arr = Enumerable.Range(1, 10).ToArray();
           
var query = arr.Select(a => a * a);
               
      foreach (var item in query)
           Console.Write(item + " ");

Output of the program is:
1 4 9 16 25 36 49 64 81 100 
 

 

 

0 comments: