+ Reply to Thread
Results 1 to 2 of 2

Thread: Generates a sequence of integral numbers within a specified range

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    3

    Generates a sequence of integral numbers within a specified range

    How can I generate a sequence of integral numbers within a specified range?

    Ex: 1, 4, 9.... (squares of number)

  2. #2
    Junior Member
    Join Date
    Oct 2012
    Posts
    4
    You can do this using Enumerable.Range Method.

    This method is implemented by using deferred execution. The immediate return value is an object that stores all the information that is required to perform the action. The query represented by this method is not executed until the object is enumerated either by calling its GetEnumerator method directly or by using foreach in Visual C# or For Each in Visual Basic.

    Example:
    Code:
             // Generate a sequence of three integers starting at 4, 
                // and then select their squares.
                IEnumerable<int> squares = Enumerable.Range(4, 3).Select(x => x * x);
    
                foreach (int num in squares)
                {
                    Console.WriteLine(num);
                }
    
                /*
                 This code produces the following output:
                 16
                 25
                 36
                */

+ Reply to Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts