C#为什么必须转换操作符必须声明为静态和公众? [英] C# why must conversion operator must be declared static and public?
问题描述
对于以下编译器错误:
用户定义的运算符Foo.implicit运营商富(酒吧)必须声明为静态的和公共
User-defined operator 'Foo.implicit operator Foo(Bar)' must be declared static and public
,这是什么原因呢?为什么一定要一个用户定义的转换操作符是公开
?
What is the reason for this? Why must a user-defined conversion operator be public
?
给出以下code,为什么不是这个转换是合法的:
Give the following code, why wouldn't this conversion be legal:
internal class Bar{}
internal class Foo
{
private Bar _myBar;
internal static implicit operator Bar(Foo foo)
{
return foo._myBar;
}
}
我查href="http://www.microsoft.com/en-us/download/confirmation.aspx?id=7029" rel="nofollow"> C#语言规格和的
I checked the C# Language Spec and the relevant section (10.10.3) didn't mention this requirement. Is it a compiler thing and not a C# / .Net restriction?
为了使事情变得怪异,我可以作出上述转换操作符公开
只要双方富
和酒吧
是内部
,这是奇怪的,因为我认为你不能返回内部在一个公开的方法
对象(但我想,如果整个类是内部
这不会有问题)。但是,如果我做一个公开
PublicFoo
并保持酒吧
内部
然后我得到一个可访问性不一致
编译器错误:
To make things weirder, I can make the above conversion operator public
as long as both Foo
and Bar
are internal
, which is odd, because I would think that you couldn't return an internal
object on a public method (although I suppose if the entire class is internal
it wouldn't matter). However, if I make a public
PublicFoo
and keep Bar
internal
then I get an inconsistent accessibility
compiler error:
internal class Bar { }
internal class Foo
{
private Bar _myBar;
/// <summary>
/// No inconsistent accessibility: return type
/// </summary>
public static implicit operator Bar(Foo foo)
{
return foo._myBar;
}
}
public class PublicFoo
{
private Bar _myBar;
/// <summary>
/// Inconsistent accessibility: return type !!
/// </summary>
public static implicit operator Bar(PublicFoo foo)
{
return foo._myBar;
}
}
摘要
为什么要用户定义的转换是公开的?
Why must user-defined conversions be public?
推荐答案
它的是的第10.10节中规定:
It is specified in §10.10:
下面的规则适用于所有运营商的声明:
The following rules apply to all operator declarations:
- 在运算符声明必须同时包含一个
公开
和静态
修改
- An operator declaration must include both a
public
and astatic
modifier.
你没有得到一个不一致的访问错误与你的第一个片段的原因是一样的道理,你没有得到一个错误,当您在一个内部类型一般定义的公共方法。这是完全有效的声明对内部类型的公共方法。
The reason you don't get an 'inconsistent accessibility' error with your first snippet is the same reason you don't get an error when you define a public method on an internal type in general. It's perfectly valid to declare public methods on internal types.
你的第二个片段得到一个错误的原因是因为它的没有的有效的在公共类的公共方法返回一个内部类。这些规则适用于任何方法,而不仅仅是运营商。
The reason you get an error in the second snippet is because it's not valid to return an internal type in a public method of a public type. These rules apply to any methods, not just operators.
至于为什么语言设计者选择需要转换经营者是公开的和hellip;我猜想他们可能想避免过多的魔力将与这些运营商&mdash;也就是说,他们不想code发挥作用的一种方式汇编和其它组件内完全不同的方式里面没有任何明显迹象对发生了什么事情。记住经营者只是语法糖。如果你想有一个转换方法,这只是你的组件中可见,简单地定义 ToBar()
和自己调用它;不依赖于运营商。
As to why the language designers chose to require conversion operators to be public… I suppose they probably wanted to avoid having too much 'magic' going on with those operators—that is, they didn't want code to function one way inside one assembly and a completely different way inside another assembly without any obvious indication of what was going on. Remember operators are just syntactic sugar. If you want to have a conversion method that's only visible within your assembly, simply define ToBar()
and call it yourself; don't rely on the operators.
这篇关于C#为什么必须转换操作符必须声明为静态和公众?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!