Skip to content

CS0027: this cannot be used in an attribute nameof #82251

Description

@vsfeedback

This issue has been moved from a ticket on Developer Community.


[severity:It bothers me. A fix would be nice]
Given a method like this:

[MemberNotNull(nameof(this.Name))]
public MyShape SetName(string name)
{
	this.Name = name;
	return this;
}
  

CS0027 will (sometimes) be emitted as an error in VS2026, with the location referring to this. as it appears in the attribute argument. Yet the compilation succeeds anyway.

CS8618 also is emitted as a false warning in this case.

Removing this. resolves both diagnostics:

[MemberNotNull(nameof(Name))]
public MyShape SetName(string name)
{
	this.Name = name;
	return this;
}
  

This looks like a bug in the compiler / language service.

Repro steps

git clone https://github.com/AArnott/Nerdbank.MessagePack.git
cd Nerdbank.MessagePack
git checkout b1e27e7a3e5954056e4f2db72d2fc1d8ce1f2053
 .\Nerdbank.MessagePack.slnx
  

Look at the error list. You may have to open MyShape.cs in order to see it populate with these two false diagnostics.


Original Comments

Feedback Bot on 12/22/2025, 03:14 AM:

We have directed your feedback to the appropriate engineering team for further evaluation. The team will review the feedback and notify you about the next steps.

Feedback Bot on 1/30/2026, 09:10 PM:

This issue is currently being investigated. Our team will get back to you if either more information is needed, a workaround is available, or the issue is resolved.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions