http://msdn.microsoft.com/zh-cn/library/85w54y0a(VS.80).aspx
代码
//卡车
class Truck
{
public Truck()
{
Desc = "卡车";
}
string _desc;
public string Desc
{
get { return _desc; }
set { _desc = value; }
}
public static explicit operator Truck(Car c)
{
Truck t = new Truck();
t.Desc = c.Desc;
return t;
}
}
//轿车
class Car
{
public Car()
{
Desc = "轿车";
}
string _desc;
public string Desc
{
get { return _desc; }
set { _desc = value; }
}
public static implicit operator Car(Truck t)
{
Car c = new Car();
c.Desc = t.Desc;
return c;
}
}
//调用
class Program
{
static void Main(string[] args)
{
Truck t = new Truck();
Car c = new Car();
Console.WriteLine("Truck:" + t.Desc);
Console.WriteLine("Car:" + c.Desc);
Console.WriteLine("explicit and implicit");
Truck t1 = (Truck)c;
Car c1 = t;
Console.WriteLine("Truck:" + t1.Desc);
Console.WriteLine("Car:" + c1.Desc);
}
}