这两个关键字可以在自己的类中,类型间转换时显式的还是隐式的
如下例子:
1
2
3
4
5
6
7
8
9
10
11
12
13
14 |
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;
原文:http://www.cnblogs.com/malc1988/p/3567687.html