dotnet / dotnet/csharpstandard

nameof can refer to a variable "early"

Open
#1,111 3 comments 0 reactions 0 assignees View on GitHub
type: bug
Dominant language
C#
Stars
815
Forks
99
Avg merge
1d 14h
Merged PRs (30d)
16

Description

Spotted in #608 by @KalleOlaviNiemitalo, but split off as it's not part of the change.

In statements.md:

> The scope of a local constant is the block in which the declaration occurs. It is an error to refer to a local constant in a textual position that precedes the end of its *constant_declarator*.

But `const string s = nameof(s);` is fine, despite the reference in `nameof(s)` being before the end of the declarator.

It's possible that we just need a carve-out for nameof in this case.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.