dotnet / dotnet/csharpstandard
Interaction of `var`, `nameof`, and `Color Color`
- Dominant language
- C#
- Stars
- 815
- Forks
- 99
- Avg merge
- 1d 14h
- Merged PRs (30d)
- 16
Description
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 and [C# Design Meeting Notes for Mar 4, 2015](https://github.com/dotnet/csharplang/blob/cd336064a26a43c31c1164ef7cd3f5feb4420d20/meetings/2015/LDM-2015-03-04.md).
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?
```csharp
class C {
void M() {
var Color = nameof(Color.Beige) == "nope" ? (Color)1 : (Color)2;
}
enum Color {
Beige,
Mauve,
Fuchsia,
}
}
```
(Continued from )
Contributor guide
Assessment
This issue has not been assessed yet.