public struct MyValue
{
private int id;
private decimal price;
public MyValue(int id, decimal price)
{
this.id = id;
this.price = price;
}
override public string ToString()
{
return String.Format("{0}: {1}", id, price);
}
unsafe public static void Swap(MyValue* pi, MyValue* pj)
{
MyValue tmp = *pi;
*pi = *pj;
*pj = tmp;
}
}
class UnsafeStructApp
{
static void Main(string[] args)
{
MyValue i = new MyValue(123, 45.67m);
MyValue j = new MyValue(890, 98.76m);
Console.WriteLine("Before Swap:\ti = {0}, j = {1}", i, j);
unsafe { MyValue.Swap(&i, &j); }
Console.WriteLine("After Swap:\ti = {0}, j = {1}", i, j);
}
}
|