c# - Swap two object when they are on two different list -
in example got 8 objects.
assume first element of bluelist refered element1 , first element of redlist1 refered element1
now want swap object1 object3, when make this:
pseudo code:
object temp = bluelist1[0]; bluelist1[0] = bluelist[1]; bluelist1[1] = temp;
the result is:
because switched list refferences. how safety swap 2 addresses of object, in picture below? can see element1 switched element3.
it easly done in c++. posible in c#?
i think need make wrapper class.
class element<t> { public t object { get; set; } public void swapwith(element<t> other) { // warning: not thread-safe t tmp = other.object; other.object = this.object; this.object = tmp; } }
then can make change like
bluelist1[0].swapwith(bluelist[1]);
Comments
Post a Comment