You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
nameof(this) in a default parameter or in an attribute argument (thus not in the function body) in a local or anonymous function in a struct
usingSystem;classDAttribute:Attribute{publicDAttribute(strings){}}structS{intj;voidLeA(){// OKAction<int>a=([D(nameof(this.j))]inti)=>{};}voidLeD(){// error CS1065, default value not allowedAction<string>action=(strings=nameof(this.j))=>{};}voidLfA(){// OKvoidF([D(nameof(this.j))]inti){}}voidLfD(){// error CS1673, cannot access this in local functionvoidF(strings=nameof(this.j)){}}}
That seems pretty inconsistent. Why is nameof(this.j) allowed in an attribute argument in the parameter list, but not in a default parameter value? Is that what the standard should specify?
Expression trees seem to allow neither attributes nor default values, so the behaviour is more consistent for them:
usingSystem;usingSystem.Linq.Expressions;classDAttribute:Attribute{publicDAttribute(strings){}}structS{intj;voidExA(){// error CS8972, attribute not allowed in expression treeExpression<Func<string,string>>e=([D(nameof(this.j))]strings)=>"";}voidExD(){// error CS1065, default value not allowed in expression treeExpression<Func<string,string>>e=(strings=nameof(this.j))=>"";}voidExR(){// error CS1673, lambda expression in struct cannot access thisExpression<Func<string,string>>e=(strings)=>nameof(this.j);}}
The text was updated successfully, but these errors were encountered:
jskeet
added
the
type: bug
The Standard does not describe the language as intended or implemented
label
Sep 13, 2023
@jskeet As you work on nameof behavior, be aware that here be (generally harmless) dragons. The behavior of nameof is one of the places where the newer compiler doesn't perfectly match older implementations wheh the LangVersion is set to an older value.
Originally reported on #926.
Some comments from @KalleOlaviNiemitalo
A surprising interaction with nameof:
To be checked:
That seems pretty inconsistent. Why is
nameof(this.j)
allowed in an attribute argument in the parameter list, but not in a default parameter value? Is that what the standard should specify?Expression trees seem to allow neither attributes nor default values, so the behaviour is more consistent for them:
The text was updated successfully, but these errors were encountered: