Use unsage code to swap two integers
public class TestUnsafeApp
{
unsafe public static void Swap(int* pi, int* pj)
{
int tmp = *pi;
*pi = *pj;
*pj = tmp;
}
public static void Main(string[] args)
{
int i = 3;
int j = 4;
Console.WriteLine("BEFORE: i = {0}, j = {1}", i, j);
unsafe { Swap(&i, &j); }
Console.WriteLine("AFTER: i = {0}, j = {1}", i, j);
}
}
Related examples in the same category