Skip to content

Interaction of var, nameof, and Color Color #832

@KalleOlaviNiemitalo

Description

@KalleOlaviNiemitalo

C# 7 draft §13.6.2 (Local variable declarations) says this about "implicitly typed local variable declaration":

The initializer expression cannot refer to the declared variable itself

As specified and implemented, this disallows also var x = nameof(x); even though the type of the expression would be unambiguous in this case. That was discussed in dotnet/roslyn#766 and C# Design Meeting Notes for Mar 4, 2015.

However, the wording "refer to the declared variable" seems a bit ambiguous in the "Color Color" case (C# 7 §12.8.7.2, Identical simple names and type names).

In the following example, does nameof(Color.Beige) refer to the variable, the type, or both?

class C {
    void M() {
        var Color = nameof(Color.Beige) == "nope" ? (Color)1 : (Color)2;
    }

    enum Color {
        Beige,
        Mauve,
        Fuchsia,
    }
}

(Continued from #831 (comment))

Metadata

Metadata

Assignees

No one assigned

    Labels

    type: bugThe Standard does not describe the language as intended or implemented

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions