public class MyValue
{
public int id;
public MyValue(int id) { this.id = id; }
}
class UnsafeClassApp
{
unsafe public static void Swap(int* pi, int* pj)
{
int tmp = *pi;
*pi = *pj;
*pj = tmp;
}
static void Main(string[] args)
{
MyValue i = new MyValue(123);
MyValue j = new MyValue(456);
Console.WriteLine("Before Swap:\ti = {0}, j = {1}", i.id, j.id);
unsafe
{
(int* pi = &i.id, pj = &j.id)
{
Swap(pi, pj);
}
}
Console.WriteLine(
"After Swap:\ti = {0}, j = {1}", i.id, j.id);
}
}
|