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

enter image description here

now want swap object1 object3, when make this:

pseudo code:

object temp = bluelist1[0]; bluelist1[0] = bluelist[1]; bluelist1[1] = temp; 

the result is:

enter image description here

because switched list refferences. how safety swap 2 addresses of object, in picture below? can see element1 switched element3.

enter image description here

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

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -