Swap two variables without using ref/out in C#
Is it possible to swap two variables without using ref/out keyword in C#(i.e. by using unsafe)?
for ex,
Answer is:
for ex,
swap(ref x,ref y);//It is possbile to by passing the reference
But, Is there any other way of doing the same thing. In the swap function you can use a temp variable. But, How to swap the variables without using ref/out keywords in C#?Answer is:
No, it's not possible to affect variables* in the caller without the use of
*(You can affect instances of reference types and the changes will be visible to the caller, but instances are not "variables".)
ref
or out
. You could affect class members, but how would you know which ones you were being asked to swap?*(You can affect instances of reference types and the changes will be visible to the caller, but instances are not "variables".)
0 comments:
Post a Comment