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));
        }
    }
}
4

2 に答える 2

7

に変換せずList<T>、列挙するだけです。

var l1 = Enumerable.Range(999900000, 1000000000);
foreach(var f in l1)
    Console.WriteLine(f);
于 2012-07-03T08:19:19.037 に答える
2

特にコンテンツがすでにわかっている場合は、リストに必要なすべてのデータを収集するのではなく、列挙子を使用して、この方法でアプリのメモリフットプリントを削減します。

例えば:

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

ループの後にこれを使用します

foreach(var integer in GetNextInt())
{ 
    //do something.. 
}
于 2012-07-03T08:21:18.497 に答える