is there any possibility to alter an array element by modifying its copy?

user4520

I'm working on quite a complex project and have been struggling to debug one problem for the last hour. I nailed down the troublesome code which looks more or less like this:

Client ClientToChange = Clients[index];
ClientToChange.Rank = 80;

Clients is a static array of classes Client, declared in the same class.

Changing ClientToChange's rank for some reason alters the contents of the array, i.e.

Console.WriteLine(Clients[index].Rank)

returns 80 (whereas before it was 100), of course index was not changed.

This seems bizarre to me, but that's what happens.

I wrote a little dummy program to test this out, just to make sure:

    static string[] foo = new string[10];
    static void Main(string[] args)
    {

        foo[3] = "bar";

        string test = "";
        test = foo[3];

        test = "fb";

        Console.Write(foo[3]);
        Console.Write(test);

This, of course, works like it should: prints barfb.

Am I doing something that I cannot see, or I don't know something I should?

Gareth Latty

When you do the assignment

Client ClientToChange = Clients[index];

You are making ClientToChange a reference to the object at Clients[index], not copying it. They are both names that happen to point to the same object. It makes sense that when you change it, you see those changes, whichever name you use (a reference in an array is just a particular type of name).

If you want a different object, you should copy it.

The reason your string case doesn't show this behaviour is because in that case, you are not modifying the contained string, but rather changing the name to point to another object. Changing what one name points to doesn't change what the other names point to. If you were to mutate the string (which you can't do, as C# strings are immutable), then you would see the change when accessing it through the array.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Copy node and alter its content

From Dev

Method for modifying an element in an Array list?

From Dev

There any possibility to add data dropping a dom element into the tree

From Dev

Copy element with its properties in mootools

From Dev

Copy element with its properties in mootools

From Dev

Local copy of array Element

From Dev

how resize an element without modifying its content. css

From Dev

modifying copy of array object affect object stored in the array

From Dev

C: Are arrays that are passed into a function creating a copy of the array or is it modifying the original array

From Dev

After removing an element from array using splice. its not resetting.Is my code has any mistake

From Dev

Modifying the javascript array and pushing it into array is copying to every element in array

From Dev

Modifying value in multidimensional array by path in array (or any) format

From Dev

Modifying value in multidimensional array by path in array (or any) format

From Dev

copying an array of objects and then modifying the original without affecting the copy

From Dev

Does "print $ARGV" alter the argument array in any way?

From Dev

CSS selector: element without any its children

From Dev

Copy to new array and remove element?

From Dev

Is there any possibility to change the location of data?

From Dev

Is there any possibility of a race condition in this code?

From Dev

Is there any possibility of a man in the middle attack?

From Java

PowerShell any element of array ends with any element in another array

From Dev

Sort array in Python without modifying specific element positions

From Dev

Why doesn't the string change when modifying string array element

From Dev

copy and alter column in CSV

From Dev

Can I specify a dimension to copy in array.copy or any alternative?

From Dev

Video doesn't update after modifying the src property of its <source> element

From Dev

In C#, sort an array by its last element

From Dev

Remove array element based on its character length

From Dev

Delete any element in Array 2 if it contains any of the elements in Array 1

Related Related

  1. 1

    Copy node and alter its content

  2. 2

    Method for modifying an element in an Array list?

  3. 3

    There any possibility to add data dropping a dom element into the tree

  4. 4

    Copy element with its properties in mootools

  5. 5

    Copy element with its properties in mootools

  6. 6

    Local copy of array Element

  7. 7

    how resize an element without modifying its content. css

  8. 8

    modifying copy of array object affect object stored in the array

  9. 9

    C: Are arrays that are passed into a function creating a copy of the array or is it modifying the original array

  10. 10

    After removing an element from array using splice. its not resetting.Is my code has any mistake

  11. 11

    Modifying the javascript array and pushing it into array is copying to every element in array

  12. 12

    Modifying value in multidimensional array by path in array (or any) format

  13. 13

    Modifying value in multidimensional array by path in array (or any) format

  14. 14

    copying an array of objects and then modifying the original without affecting the copy

  15. 15

    Does "print $ARGV" alter the argument array in any way?

  16. 16

    CSS selector: element without any its children

  17. 17

    Copy to new array and remove element?

  18. 18

    Is there any possibility to change the location of data?

  19. 19

    Is there any possibility of a race condition in this code?

  20. 20

    Is there any possibility of a man in the middle attack?

  21. 21

    PowerShell any element of array ends with any element in another array

  22. 22

    Sort array in Python without modifying specific element positions

  23. 23

    Why doesn't the string change when modifying string array element

  24. 24

    copy and alter column in CSV

  25. 25

    Can I specify a dimension to copy in array.copy or any alternative?

  26. 26

    Video doesn't update after modifying the src property of its <source> element

  27. 27

    In C#, sort an array by its last element

  28. 28

    Remove array element based on its character length

  29. 29

    Delete any element in Array 2 if it contains any of the elements in Array 1

HotTag

Archive