Replies: 6 comments 2 replies
-
This does seem like a strange way to specify what is allowed. There are plenty of expressions for which |
Beta Was this translation helpful? Give feedback.
-
Just because the argument of Syntactically, |
Beta Was this translation helpful? Give feedback.
-
Hold on, but what about this?
Screenshot proof in VS2017, compiler set to C# 7.3: Why are namespace names with at least two components allowed, but not single name components? |
Beta Was this translation helpful? Give feedback.
-
Syntactically (and syntax is all that matters here), On the other hand, |
Beta Was this translation helpful? Give feedback.
-
Came up here: https://stackoverflow.com/questions/69116668/why-can-nameof-not-be-used-with-alias-qualified-types-at-the-root-level Sorry to resurrect, but to add: Keep in mind that // note: no namespace here
public class N
{
public class C {}
} Here,
It isn't a valid |
Beta Was this translation helpful? Give feedback.
-
while we're at it, let's get e.g.
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
@Przemyslaw-W commented on Tue Aug 29 2017
Hi,
Consider code:
First statement compiles just fine, second emits error. Why is handling of namespace and class different here?
@svick commented on Tue Aug 29 2017
It's not. The error is "error CS8083: An alias-qualified name is not an expression." And that's the issue: syntactically, the code inside
nameof
has to be an expression.A
,A.B
andglobal::A.B
are all syntactically valid expressions, butglobal::A
is not.So, the difference isn't between namespace and class, it's about the exact syntax used. This means that
nameof(global::N.M)
is valid, even thoughN.M
is a namespace. Andnameof(global::C)
is not valid, even for classC
that's in the global namespace.The decision to make
nameof
accept only code that's syntactically an expression means it has some other annoying restrictions (see #702). If lifting those restrictions was considered, maybe theglobal::N
case could be considered at the same time?@Przemyslaw-W commented on Wed Aug 30 2017
Now I can see what the issue is, thanks.
Imo this restriction seems a bit artificial and alias qualified names should be legal inside nameof. Please treat this issue as vote up for lifting these restrictions.
Beta Was this translation helpful? Give feedback.
All reactions