Reputation: 1344
I know there are limitations to the number of items that can be added to a List (basically int.MaxValue), but what is a reasonable number to use in an everyday scenario - 100k, 500k, 1 million?
I'm using a DataReader and adding one string at a time to a List, then processing the elements as a batch. I want to ensure I'm not pushing memory limits too much by making the batches too large.
Upvotes: 2
Views: 145
Reputation: 35477
Another approach is to return an IEnumerable (not List) and use the yield
keyword. This will not consume extra memory for a list.
public IEnumerable<string> MyBigList(DataReader reader)
{
while (reader.Read())
{
yield return (string) reader["SomeField"];
}
}
In the old days this was called a co-routine, in C# its referred to as an iterator. See MSDN for more information on iterators and yield
.
To process the string(s)
foreach (var s in MyBigList(reader))
{
// do something with s
}
Upvotes: 6
Reputation: 34189
It depends.
The main idea behind the size of count of strings is that every character takes 2 bytes of memory by default.
Some considerations:
List
for every user request, then it is going to grow exponentially. O(N)
operations. Probably, you would like to change used collection type according to your problem or use a database. List
at all. In most cases, "What is the reasonable number of List
items" question is not as critical as
"Is this a reasonable case for using List
at all?".
In-memory dynamic-size plain collections (i.e., List
) can either be useful or useless in different usage scenarios, and we can't know which one is your case (especially, without your code).
Upvotes: 2