user4729970
user4729970

Reputation:

How many objects of some specific class I can add to List

I know that number of items you can add to List in C# is limited by int (because that is the type of count). Which should be around 2 billion.

But my question is say I want to have such list:

List<MyClass>= new List<MyClass>;

Where

class MyClass
{
  string s1; // should be string of 8 characters always
  DateTime t;
}

How many objects of MyClass I can add to the list? I assume now I have to take into account that the memory it will occupy is numberOfElements * sizeof(MyClass)? So how many elements I can add using such constraint? (I believe I may run out of memory faster than I reach max value of int due to formula above, isn't it?).

Upvotes: 1

Views: 434

Answers (1)

D Stanley
D Stanley

Reputation: 152521

How many objects of MyClass I can add to the list? I assume now I have to take into account that the memory it will occupy is numberOfElements * sizeof(MyClass)?

Since MyClass is a reference type, the list itself will just contain references to objects, so the maximum size of the list will not be any different.

I believe I may run out of memory faster than I reach max value of int due to formula above, isn't it?

You may run out of memory, yes, but it's not a limitation of List<T>. You are only limited by the amount of virtual memory available.

Upvotes: 5

Related Questions