0

I asked a question similar to this one a few days ago: C# Does an array of objects store the pointers of said objects?

Now, what I'm asking is a bit more code related. A brief overview, I'm trying to store many objects into a list and the said objects may be very large (1 ~ 2 GB). From my research, a .NET data structure only has 2 GB max of memory. So my question is, am I able to fit more than 1 or 2 objects into a list?

Scenario: I have a human class and I'm trying to store it in a list. Are these two lines of code different?

List<Human> humanList = new List<Human>();
Human human = new Human(attr1, attr2, attr3);
humanList.Add(human)

vs.

List<Human> humanList = new List<Human>();
humanList.Add(new Human(attr1, attr2, attr3))

Is the first set of code using a reference to the human object so I'll be able to store more objects in the list? Similarly, is the second set of code trying to store the whole human object into the list?

Community
  • 1
  • 1
user3085290
  • 285
  • 1
  • 2
  • 10
  • 1
    The list will store references (akin to pointers in C-style languages). Your two code samples are equivalent. – D Stanley Jul 08 '15 at 14:14
  • Question: are you talking about a 32-bit compiled output, or 64-bit? Under 4.5 using 64-bit output, the 2GB limit is dead. https://msdn.microsoft.com/en-us/library/ms241064(v=vs.110).aspx and https://msdn.microsoft.com/en-us/library/hh285054(v=vs.110).aspx – code4life Jul 08 '15 at 14:18
  • the `new` operator creates an object on the heap and returns a **reference** to that object. So `human` and `humanList[0]` are both **references** to the same object. – Dennis_E Jul 08 '15 at 14:19
  • Oh I understand a lot clearer now, thanks guys :D I'm currently using 4.0, but I'll do a little more research into the 2 GB limit. Thanks again – user3085290 Jul 08 '15 at 14:21

3 Answers3

3

Your two code samples are equivalent (assuming you aren't doing anything else with the human variable after you create it). The new keyword creates a reference which is stored in the list - you're just creating that reference inline instead of storing it in a variable.

Assuming Human is a class and not a struct, the list will contain references to the objects, not the objects themselves.

The size of a Human object will have less bearing on how many references you can store in the list (overall system memory will likely be more of an issue that the 2GB .NET limit).

D Stanley
  • 149,601
  • 11
  • 178
  • 240
1

Both code examples have the same effect.

Both examples store references to your objects.

You will be able to store many more than 1 or 2 objects in the list.

tomsv
  • 7,207
  • 6
  • 55
  • 88
  • Agreed - and a quick look at the decompiled code via Reflector would have revealed the same thing. Anyways, upvoted. – code4life Jul 08 '15 at 14:19
0

This gets managed by the .Net CLR and code optimization. Your code would be changed in either scenario to store a reference to the human object. The only difference here is that the first example has a hook to the reference for the local method.

oppassum
  • 1,746
  • 13
  • 22