为什么我不能使用带显式运算符的接口? [重复]

时间:2021-08-16 06:53:22

This question already has an answer here:

这个问题在这里已有答案:

I'm just wondering if anyone knows the reason why you are not allowed to use interfaces with the implicit or explicit operators?

我只是想知道是否有人知道你不允许使用隐式或显式运算符的接口的原因?

E.g. this raises compile time error:

例如。这会引发编译时错误:

public static explicit operator MyPlayer(IPlayer player)
{
 ...
}

"user-defined conversions to or from an interface are not allowed"

“不允许在接口之间进行用户定义的转换”

Thanks,

谢谢,

1 个解决方案

#1


30  

Section 10.9.3 of the C# spec spells this out. The short version is that it's disallowed so that the user can be certain that conversions between reference types and interfaces succeed if and only if the reference type actually implements that interface, and that when that conversion takes place that the same object is actually being referenced.

C#规范的第10.9.3节解释了这一点。简短版本是不允许的,以便用户可以确定引用类型和接口之间的转换是成功的,当且仅当引用类型实际实现该接口时,并且当转换发生时实际引用相同的对象时。

Defining an implicit or explicit conversion between reference types gives the user the expectation that there will be a change in reference; after all, the same reference cannot be both types. On the other hand, the user does not have the same expectation for conversions between reference types and interface types.

定义引用类型之间的隐式或显式转换使用户期望引用会发生更改;毕竟,相同的参考不能是两种类型。另一方面,用户对引用类型和接口类型之间的转换没有相同的期望。

User-defined conversions are not allowed to convert from or to interface-types. In particular, this restriction ensures that no user-defined transformations occur when converting to an interface-type, and that a conversion to an interface-type succeeds only if the object being converted actually implements the specified interface-type.

不允许用户定义的转换从接口类型转换或转换为接口类型。特别是,此限制确保在转换为接口类型时不会发生用户定义的转换,并且只有在转换的对象实际实现指定的接口类型时,才能成功转换为接口类型。

#1


30  

Section 10.9.3 of the C# spec spells this out. The short version is that it's disallowed so that the user can be certain that conversions between reference types and interfaces succeed if and only if the reference type actually implements that interface, and that when that conversion takes place that the same object is actually being referenced.

C#规范的第10.9.3节解释了这一点。简短版本是不允许的,以便用户可以确定引用类型和接口之间的转换是成功的,当且仅当引用类型实际实现该接口时,并且当转换发生时实际引用相同的对象时。

Defining an implicit or explicit conversion between reference types gives the user the expectation that there will be a change in reference; after all, the same reference cannot be both types. On the other hand, the user does not have the same expectation for conversions between reference types and interface types.

定义引用类型之间的隐式或显式转换使用户期望引用会发生更改;毕竟,相同的参考不能是两种类型。另一方面,用户对引用类型和接口类型之间的转换没有相同的期望。

User-defined conversions are not allowed to convert from or to interface-types. In particular, this restriction ensures that no user-defined transformations occur when converting to an interface-type, and that a conversion to an interface-type succeeds only if the object being converted actually implements the specified interface-type.

不允许用户定义的转换从接口类型转换或转换为接口类型。特别是,此限制确保在转换为接口类型时不会发生用户定义的转换,并且只有在转换的对象实际实现指定的接口类型时,才能成功转换为接口类型。