Storing large amounts of object references

G

Gustaf

Just want to make sure I got this right...

Let's say I have a large array of A objects. Each A object has a
property that contains a reference to a B object. It would be possible
to store the property simply as a string, but I rather make the whole B
object available there. All the A objects may refer to the same B object.

To my understanding, when C# stores the reference to B objects in the A
object property, it just stores an address in memory, not the whole
object itself. So looping through the long list of A objects shouldn't
be slower than if I store the property as a string, right?

Gustaf
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

To my understanding, when C# stores the reference to B objects in the A
object property, it just stores an address in memory, not the whole object
itself. So looping through the long list of A objects shouldn't be slower
than if I store the property as a string, right?

It does store the address in memory or in other words a reference.
 
B

Bruce Wood

Right... so long as B is declared as a "class" and not as a "struct".
If B is declared as a "struct" then every A will have a separate copy
of B embedded in it. If B is declared as a "class" (which is what you
want) then every A will contain a reference (pointer) to its associated
B, and A's may share one or more B's.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top