C#编译器是否使用const类型成员弄错了Color Color规则? [英] Does the C# compiler get the Color Color rule wrong with const type members?

查看:96
本文介绍了C#编译器是否使用const类型成员弄错了Color Color规则?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,因此C#语言规范具有特殊部分(已链接旧版本))上的 Color Color 规则,其中成员及其类型具有相同的名称.著名大师Eric Lippert

Okay, so the C# Language Specification has a special section (old version linked) on the Color Color rule where a member and its type has the same name. Well-known guru Eric Lippert once blogged about it.

我在这里要问的问题在某种意义上(不是)与线程常量枚举.如果愿意,您可以投票赞成另一个问题.

The question I am going to ask here is in a sense (not) quite the same that was asked in the thread Circular definition in a constant enum. You can go and upvote that other question if you like.

现在我的问题.考虑以下代码:

Now for my question. Consider this code:

namespace N
{
    public enum Color
    {
        Green,
        Brown,
        Purple,
    }

    public class C1
    {
        public const Color Color = Color.Brown;  // error CS0110 - WHY? Compiler confused by Color Color?
    }
    public class C2
    {
        public static readonly Color Color = Color.Brown;  // fine
    }
    public class C3
    {
        public static Color Color = Color.Brown;  // fine
    }
    public class C4
    {
        public Color Color = Color.Brown;  // fine
    }
}

这里的要点是,在上述每种情况下,最右边的标识符 Color 可以引用 enum 类型,也可以引用具有相同名称的类成员.但是上面提到的 Color Color 规则意味着我们应该查看成员( Brown )是静态还是非静态.由于在这种情况下它是静态的,因此我们应该相应地解释 Color .

The point here is that in each situation above, the right-most identifier Color can refer either to the enum type, or to the class member with the same name. But the Color Color rule mentioned above means that we should see if the member (Brown) is static or non-static. Since it is static in this case, we should interprete Color accordingly.

我显而易见的主要问题:为什么这不适用于 const 类型的成员?这是意想不到的吗?

My obvious main question: Why does this not work with a const type member? Is this unintended?

(很明显,说 N.Color.Brown ( N 是名称空间)修复"它;我不是在问这个!)

(Obviously, saying N.Color.Brown (N is the namespace) "fixes" it; I am not asking about that!)

侧面说明:使用局部变量 const 时,上述异常不存在:

Side note: With local variable const, the above anomaly does not exist:

    public class C5
    {
        public Color Color;
        void M()
        {
            const Color Color = Color.Brown;  // works (no warning for not using local variable?)
        }
    }
    public class C6
    {
        public Color Color;
        void M()
        {
            const Color other = Color.Brown;  // works (warning CS0219, 'other' not used)
        }
    }

推荐答案

这是一个错误.我无法在VS 2015的CTP 5中重现该问题,并且我认为应该在Roslyn重写中解决此问题.但是,下面的评论者指出,他们可以在CTP 6中重现它.因此,就此错误是否已修复而言,我不确定这里发生了什么.

It is a bug. I am unable to reproduce the problem in CTP 5 of VS 2015, and I think this one should have been fixed as part of the Roslyn rewrite. However, a commenter below notes that they can reproduce it in CTP 6. So I'm not sure what is going on here as far as whether this bug has been fixed or not.

就我个人而言:我不特别记得我是否曾在2010年首次报道该任务时研究它,但由于我当时在圆度检测器上做了很多工作,因此赔率非常好.

On a personal note: I do not specifically recall if I was tasked with looking into this one when it was first reported back in 2010 but since I did quite a bit of work on the circularity detectors around then, odds are pretty good.

这远非圆度检测器中的唯一错误;如果存在嵌套的泛型类型,而这些泛型类型又具有泛型基类型,而泛型基类型的类型实参涉及嵌套的类型,则会非常困惑.

This is far from the only bug there was in the circularity detectors; it would get quite confused if there were nested generic types which in turn had generic base types whose type arguments involved the nested types.

亚历克斯不会解决"这个问题,我一点也不感到惊讶.我花了很长时间重写了进行类圆度检测的代码,并且认为更改风险太大.所有的工作都交给罗斯林.

I am not at all surprised that Alex "won't fix"ed this one; I spent quite a long time rewriting the code that did class circularity detection and the change was deemed too risky. All that work was punted to Roslyn.

如果您有兴趣了解Roslyn中Color Color绑定代码的工作方式,请查看适当命名的方法

If you're interested to see how the Color Color binding code works in Roslyn, take a look at the aptly-named method BindLeftOfPotentialColorColorMemberAccess -- I love me some descriptive method names -- in Binder_Expressions.cs.

这篇关于C#编译器是否使用const类型成员弄错了Color Color规则?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆