C#关键字,explicit和implicit
这两个关键字可以在自己的类中,类型间转换时显式的还是隐式的
如下例子:
public static implicit operator float(Currency value)
{
return value.dollars + (value.cents / 100.0f);
}
public static explicit operator Currency(float value)
{
checked
{
uint dollars = (uint)value;
ushort cents = Convert.ToUInt16((value - dollars) * 100);
return new Currency(dollars, cents);
}
}
public static implicit operator float(Currency value)
表示Currency到float是隐式转换,如:
Currency a;
float b = a;
public static explicit operator Currency(float value)
表示float到Currency需要强制转换,如:
float a;
Currency b = (Currency)a;
浙公网安备 33010602011771号