explicit 关键字用于声明必须使用强制转换来调用的用户定义的类型转换运算符。例如,在下面的示例中,此运算符将名为 Fahrenheit 的类转换为名为 Celsius 的类:

 CopyCode image复制代码
// 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));
}

可以如下所示调用此转换运算符:

 CopyCode image复制代码
Farenheit f = new Farenheit(100.0f);
Celsius c = (Celsius)f;

Expand 图像备注

Expand 图像示例

Expand 图像C# 语言规范

Expand image请参见