Conceptual question--How do collections store objects in .NET ?

  • Thread starter Thread starter Kevin
  • Start date Start date
K

Kevin

I'm missing a basic understanding of how .net stores objects in a
collection. For example, I have an object called "dog", and I would
like to create and populate a List<> of these.

For example:.
dog poodle =new dog("small", "white", 1);
dog shepherd =new dog("big", "brown", 2);
dog lab =new dog("medium", "black",3);

List<dog> allDogs=new List<dog>();

allDogs.Add(poodle);
allDogs.Add(shepherd);
allDogs.Add(lab);

Now, I would like a second list that overlaps with part of the first
list:

List<dog> bigDogs=new List<dog>();
bigDogs.Add(shepherd);

What exactly is hapening here? Is 'bigDogs' merely getting a
reference to 'shepherd', or is an entirely new copy of 'shepherd'
created for inclusion in the 'bigDogs' list?

If I change one item in the allDogs list (say, allDogs[1].color =
"Black") this change is not reflected in the bigDogs list. I must
assume that there are now multiple copies of shepherd...

This is unfortunate. Is there a way to create a collection that
simply refers to objects by reference? Or, put another way, is there
any way to have two overlapping lists (or some other collection) so
that a change to an item in one list is reflected in the other
list?

Thanks in advance.
-Kevin
 
Kevin said:
I'm missing a basic understanding of how .net stores objects in a
collection. For example, I have an object called "dog", and I would
like to create and populate a List<> of these.

For example:.
dog poodle =new dog("small", "white", 1);
dog shepherd =new dog("big", "brown", 2);
dog lab =new dog("medium", "black",3);

List<dog> allDogs=new List<dog>();

allDogs.Add(poodle);
allDogs.Add(shepherd);
allDogs.Add(lab);

Now, I would like a second list that overlaps with part of the first
list:

List<dog> bigDogs=new List<dog>();
bigDogs.Add(shepherd);

What exactly is hapening here? Is 'bigDogs' merely getting a
reference to 'shepherd', or is an entirely new copy of 'shepherd'
created for inclusion in the 'bigDogs' list?
The first, it adds a reference.
Unless its a value type (or immutable ref type) then it will add a copy.
Is dog a struct?
If I change one item in the allDogs list (say, allDogs[1].color =
"Black") this change is not reflected in the bigDogs list. I must
assume that there are now multiple copies of shepherd...
There must be something strange going on there.
Set shepherd.color="Black" and check all three references to see if
they've changed, they will have unless something else is going on.

<...>

JB
 
What exactly is hapening here? Is 'bigDogs' merely getting a
reference to 'shepherd', or is an entirely new copy of 'shepherd'
created for inclusion in the 'bigDogs' list?

If "dog" is a class, then List stores the references. If dog is a struct
(which is apprarently your case), then the lists will contain independent
copies of the objects.

--

Kevin said:
I'm missing a basic understanding of how .net stores objects in a
collection. For example, I have an object called "dog", and I would
like to create and populate a List<> of these.

For example:.
dog poodle =new dog("small", "white", 1);
dog shepherd =new dog("big", "brown", 2);
dog lab =new dog("medium", "black",3);

List<dog> allDogs=new List<dog>();

allDogs.Add(poodle);
allDogs.Add(shepherd);
allDogs.Add(lab);

Now, I would like a second list that overlaps with part of the first
list:

List<dog> bigDogs=new List<dog>();
bigDogs.Add(shepherd);

What exactly is hapening here? Is 'bigDogs' merely getting a
reference to 'shepherd', or is an entirely new copy of 'shepherd'
created for inclusion in the 'bigDogs' list?

If I change one item in the allDogs list (say, allDogs[1].color =
"Black") this change is not reflected in the bigDogs list. I must
assume that there are now multiple copies of shepherd...

This is unfortunate. Is there a way to create a collection that
simply refers to objects by reference? Or, put another way, is there
any way to have two overlapping lists (or some other collection) so
that a change to an item in one list is reflected in the other
list?

Thanks in advance.
-Kevin
 
Kevin said:
I'm missing a basic understanding of how .net stores objects in a
collection. For example, I have an object called "dog", and I would
like to create and populate a List<> of these.
For example:.
dog poodle =new dog("small", "white", 1);
dog shepherd =new dog("big", "brown", 2);
dog lab =new dog("medium", "black",3);
List<dog> allDogs=new List<dog>();

Now, I would like a second list that overlaps with part of the first
list:
List<dog> bigDogs=new List<dog>();
bigDogs.Add(shepherd);
What exactly is hapening here? Is 'bigDogs' merely getting a
reference to 'shepherd', or is an entirely new copy of 'shepherd'
created for inclusion in the 'bigDogs' list?

The first, it adds a reference.
Unless its a value type (or immutable ref type) then it will add a copy.
Is dog a struct?
If I change one item in the allDogs list (say, allDogs[1].color =
"Black") this change is not reflected in the bigDogs list. I must
assume that there are now multiple copies of shepherd...

There must be something strange going on there.
Set shepherd.color="Black" and check all three references to see if
they've changed, they will have unless something else is going on.

<...>

JB

Hi JB

Thank you for the clarification.

You are in fact correct--there was something strange going on, and it
was human error on my part. After walking away and coming back, I
found my error and discovered that yes, as you stated, the change was
being reflected properly. Some days trying harder just makes things
worse.

Thanks for your help and explanation.
-Kevin
 
The first, it adds a reference.
Unless its a value type (or immutable ref type) then it will add a copy.
Is dog a struct?

Immutable reference types are just like mutable reference types - no
copy is taken. It just so happens that you can't change the data within
the object. Indeed, it would be supremely pointless to take a copy of
the object given that its contents can't be changed anyway!
 
Jon said:
Immutable reference types are just like mutable reference types - no
copy is taken. It just so happens that you can't change the data within
the object. Indeed, it would be supremely pointless to take a copy of
the object given that its contents can't be changed anyway!
Doh!
Right as always :D

Thanks

JB
 
use a database you ****ing idiot


I mean seriously here; arrays and all this other crap are a load of BS
 
Back
Top