keyword - Swap two variables without using ref/out in C# -


is possible swap 2 variables without using ref/out keyword in c#(i.e. using unsafe)?

for ex,

        swap(ref x,ref y);//it possbile passing reference 

but, there other way of doing same thing. in swap function can use temp variable. but, how swap variables without using ref/out keywords in c#?

a (very!) contrived example using delegates:

class program {     static void funkyswap<t>(t a, t b, action<t> seta, action<t> setb)     {         t tempa = a;         seta(b);         setb(tempa);     }     static void main(string[] args)     {         string s1 = "big";         string s2 = "apples";         console.writeline("before, s1: {0}, s2: {1}", s1, s2);         funkyswap(s1, s2, => s1 = a, b => s2 = b);         console.writeline("after,  s1: {0}, s2: {1}", s1, s2);     } } 

while above pretty silly, using delegate setter method can useful in other situations; i've used technique implementing undo/redo of property modifications.


Comments

Popular posts from this blog

ASP.NET/SQL find the element ID and update database -

jquery - appear modal windows bottom -

c++ - Compiling static TagLib 1.6.3 libraries for Windows -