0

How should I make a list which can accommodate this range(in the code) since it is showing out of memory exception?

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            var l1 = Enumerable.Range(999900000, 1000000000).ToList();
            l1.ForEach(f => Console.WriteLine(f));
        }
    }
}
2

2 Answers 2

7

Don't convert to List<T>, just enumerate:

var l1 = Enumerable.Range(999900000, 1000000000);
foreach(var f in l1)
    Console.WriteLine(f);
Sign up to request clarification or add additional context in comments.

1 Comment

Exactly what lazy evaluation was made for :-)
2

Do not collect all data you need in the list especially if you know already the content of it, but use enumerator, to reduce in this way memory footprint of your app.

For example:

    IEnumerable<int> GetNextInt()
    {
        for(int i=999900000; i< 1000000000; i++)
        {
            yield return i;
        }
    }

and use this after in loop like

foreach(var integer in GetNextInt())
{ 
    //do something.. 
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.