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
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