This article will shortly present two ways of swapping variables in C#. Specifically, these two ways are only both available for value types and structs and unmanaged types, while the managed swap by using ref is available for values, structs and managed structs.
The method
UnsafeSwap swaps two variables by using
unsafe code and pointers. By passing in the address of the two variables to swap, it is possible to use the dereference operator * to not only copy into
a temporary variable but also use the syntax *a = *b to exchange the address the variable is pointing to, effectively we swap to variables with their content, here int is used.
Another way is do a
SafeSwap where we pass in the ref of the variables and just change their contents.
Note that if you want to exchange two strings, you must pin the pointer of the chars using the
fixed keyword.
unsafe void Main()
{
int one = 20;
int two = 30;
Console.WriteLine("Before swap, one: {0}, two: {1}", one, two);
UnsafeSwap(&one, &two);
Console.WriteLine("Unsafe swap, one: {0}, two: {1}", one, two);
SafeSwap(ref one, ref two);
Console.WriteLine("Safe swap back again, one: {0}, two: {1}", one, two);
}
unsafe void UnsafeSwap(int* a, int* b){
int temp = *a;
*a = *b;
*b = temp;
}
void SafeSwap(ref int a, ref int b){
int temp = a;
a = b;
b = temp;
}
To exchange two string variables, we must use the
fixed keyword and of course only be able to exchange the characters from the char array consisting the first variable to be exchanged with the
char array of the second variable. In addition, the string in C# is at a low level actually a character array null terminated with the '\0' sequence, at least when a string is treated as a character array..
The following method will exchange two string variables using unsafe / unmanaged code. Note that the two strings differ in length, so the last character is not exchanged. That is - our string is actually
a character
array and consists of multiple memory addresses.
unsafe void Main()
{
fixed (char* one = "hello??")
{
fixed (char* two = "world!")
{
char* ptrone = one;
char* ptrtwo = two;
while (*ptrone != '\0' && *ptrtwo != '\0')
{
Swap(ptrone, ptrtwo);
Console.WriteLine("one: {0}", GetString(one));
Console.WriteLine("two: {0}", GetString(two));
++ptrone;
++ptrtwo;
}
}
}
}
unsafe void Swap(char* first, char* second){
char temp = *first;
*first = *second;
*second = temp;
}
unsafe string GetString(char* input){
char* ptr = input;
var sw = new StringBuilder();
while (*ptr != '\0')
{
sw.Append(ptr->ToString());
++ptr;
}
return sw.ToString();
}
The output in Linqpad gives as the exchange of the string or actually a null terminated character array progresses:
one: wello??
two: horld!
one: wollo??
two: herld!
one: worlo??
two: helld!
one: worlo??
two: helld!
one: world??
two: hello!
one: world!?
two: hello?