.NET C# Generic List changing data inside a list member not working as expected and a work around.

OK so this is a very weird problem. I spent hours debugging this one. Basically the problem is as follows:

You have generic list like this one:

List<Genome> Genomes;

You access a member object inside the list and use a member setter to change a value inside the object:

for (int gen = 0; gen < this.Genomes.Length; ++gen)

You would expect this to work. All it does is takes a integer number from a list of values and passes it to be set into the desired chld object in a generic list. This simply did not work.

What I found out was that I was adding a genome for the list above to another list to have a connection to this genome:


Later I would create a new list of genomes:

List<Genome> NewGenomes;

Add new members to the list:

Genome baby = null; // And do something with the baby object

NewPop[currentNewGenomeIndex]= baby;

NOTICE: The baby was populated either through an algorithmic logic which took a pre-existing genome from the species list above or created a new one a crossover between two existing genomes in the species list.

Now here where the problem was. Something is happening in adding and getting object from the species list using existing object of same type from the genomes list. Then later when I create a new list of genomes I simply just replace the current list of genomes with the new population of genomes.

this.Genomes = NewGenomes;

This caused weird behaviour when I was trying to access a object member from the Genomes list above. For starters I would only be able to access only one object which was one and the same for the entire list. Other weird behaviour was when I was trying to loop through the members in the genomes list I was able to modify the values but once I left the loop the data was all wrong again.

Anyway very weird. What I did was simply to quickly fix it was to create an array instead of a generic list. This fixed it. And no using the generic list member function ToArray() did not help either. There was still some weird references going on. Sample of the fix:

Genome[] Genomes;

NewPop[currentNewGenomeIndex]= baby;

(I’m lazy here, please forgive me :), I assume that you know arrays and how to use them as with C#)

Something clearly is happening between two generic lists sharing the same object data.

Not sure even now what is really going on. Anyway using an Array should be more effective than using generic lists, performance wise. Although generic lists do offer flexibility in objects creation and management.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s