User-725366812 posted
Hi, Jalpesh,
This is very interesting, I agree yield is very powerful command. But your post is a little difficult to read, so here is it again cleaned up. Also changed the < to <=, I think it is more sense that the Square(1, 10) should go from 1...100
rather than 1...81:
public static IEnumerable<int> Square(int min, int max)
{
for (int i = min; i <= max; i++)
{
yield return i*i;
}
}
Now each time this method is called it will return the square of current value within a given range and its also maintains the state between calls. Let create for each loop to call the the square function above.
foreach (int i in Square(1, 10))
{
Response.Write(i.ToString() + " ");
}
And output will be like 1,4,9,16,25,36,49,64,81,100.
Regards,
~ mellamokb