explicit 关键字用于声明必须使用强制转换来调用的用户定义的类型转换运算符。例如,在下面的示例中,此运算符将名为 Fahrenheit 的类转换为名为 Celsius 的类:
// Must be defined inside a class called Farenheit:
public static explicit operator Celsius(Farenheit f)
...{
return new Celsius((5.0f/9.0f)*(f.degrees-32));
}
可以如下所示调用此转换运算符:下面的示例提供Farenheit f = new Farenheit(100.0f);
Celsius c = (Celsius)f;
Fahrenheit
和 Celsius
类,它们中的每一个都为另一个提供显式转换运算符。



























































