As mentioned in solution 1, the easiest way to do it would be to use
yield
operator.
As mentioned in solution 3 and in commented code below, there are a lot of possible improvements that should be made for code. In fact, your code would not even compile.
But since
Linq
already have an extension method for that, it would be even simpler to reuse that instead of reinventing the wheel.
So something like:
using System.Linq;
public class MainWindow()
{
private const int itemCount = 100000;
private void ShowNumbers()
{
foreach (var item in Enumerable.Range(0, itemCount))
{
txtbox1.Text = item.ToString("N0");
comboBox.Items.Add(item.ToString("N0"));
}
}
}
Alternatively, if you want your own enumerator (maybe you want something more complex), then the following code would do:
class Numbers
{
public IEnummerable<int> MyList
{
get
{
for (int i = 0; i < 100000; i++)
{
yield return i;
}
}
}
}