0

I am a little confused about why the logic here isn't working, and I feel like I have been staring at this bit of code for so long that I am missing something here. I have this method that gets called by another method:

  private async Task<bool> Method1(int start, int end, int increment, IEnumerable<ModelExample> examples)
        {
                for (int i = start; i<= end; i++)
                {
                   ModelExample example = examples.Where(x => x.id == i).Select(i => i).First();
                   example.id = example.id + increment; //Line X
                   // do stuff

                }

                return true;

        }

I debuged the code above and it seems like when "Line X" gets executed not only is example.id changed individually but now that example in the List "examples" gets updated with the new id value. I am not sure why? I want the list "examples" to remain the same for the entirety of the for loop, I am confused why updating the value for example.id updates it in the list as well?

(i.e. if the list before "Line X" had an entry with id = 0, after "Line X" that same entry has its id updated to 1, how can I keep the variable "examples" constant here?)

Any help appreciated, thanks.

  • 2
    Most likely `ModelExample` is a `class`? In which case your `example` is a reference to the object in the `IEnumerable`, not a copy – UnholySheep Mar 21 '22 at 10:00
  • `examples.Where(x => x.id == i).Select(i => i).First();` Why are you assuming that the id always exist? – Jeroen van Langen Mar 21 '22 at 10:02
  • 1
    By the way `example = examples.First(x => x.id == i);` will be fine – Caius Jard Mar 21 '22 at 10:11
  • Would you have queried "why does the person's age increment when I do this? `Console.Write(people[0].Age); var p = peopleArray[0]; p.Age += 1; Console.Write(people[0].Age);` " - it's the same thing – Caius Jard Mar 21 '22 at 10:13
  • *how can I keep the variable "examples" constant here?* - keep it constant by not varying it. If you want C#s help there are various approaches you can take to prevent `id`'s value being modified – Caius Jard Mar 21 '22 at 10:15
  • Got it! Thanks everyone! – Sdeeeeeeeeeeee Mar 21 '22 at 10:15

1 Answers1

6

This is what your list looks like:

+---------------+   
| List examples |           +-----------+
+---------------+           |  Example  |
|               |           +-----------+
|     [0] --------------->  | Id: 1     |
|               |           | ...       |
+---------------+           +-----------+
|               |           
|     [1] --------------->  +-----------+
|               |           |  Example  |
+---------------+           +-----------+
|               |           | Id: 2     |
|     ...       |           | ...       |
|               |           +-----------+
+---------------+

In other words, your list just contains references to your examples, not copies. Thus, your variable example refers to one of the entities on the right-hand side and modifies it in-place.

If you need a copy, you need to create one yourself.

Heinzi
  • 167,459
  • 57
  • 363
  • 519