nameof
should support keyword type aliases
#7997
Replies: 1 comment
-
Note: this is intentional. The purpose of nameof is to give the actual name of something. Note that Also,
All types are expressions in the language. That's what enables writing a type on the left hand side of a
Sure, but Having the rules be simple/clear/consistent is def valuable here. When you see As mentioned above, this is also very relevant for aliases. if you have |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
nameof
is a contextual keyword. As such, it is prone to sharp edges such as cryptic error messages when used incorrectly.For example,
nameof(int)
gives CS1525: "[i]nvalid expression term 'int'". This is confusing as I didn't write an expression, but a type keyword. It gets worse asnameof(int.TryParse)
works just fine (gives "TryParse"). If I wantnameof(int)
to give "Int32", I must writenameof(Int32)
.This also clashes with the existing lookup rules for
typeof
;typeof(int)
returns the same astypeof(System.Int32)
.An open question would be: should
nameof(int)
give "int" or "Int32". I'd argue the latter, as "name of" returns "the name of a program entity as a constant string", and "int" does not exist at runtime. So it's not an "entity", but a keyword.Beta Was this translation helpful? Give feedback.
All reactions