1

In Visual Studio 2019, when writing .net (C# or VB.net), is there a way to automatically prefix members/ properties of an object with their respective "self" reference?

Eg in c#, automatically prepending "this." when referencing members of a class from within the class.

Eg in VB.net, automatically prepending "me." when referencing members of a class from within the class.

Using an extension to do this is fine, if one exists.

3
  • so you have already written code, and now you want to write automatically the this. prefix in front of every property?
    – Mong Zhu
    Jan 14, 2020 at 17:00
  • @MongZhu yes. That is correct. Jan 14, 2020 at 17:01
  • When you have set the Qualify field/property/method in the language Code Style options, you can use Edit-Advanced-Format Document or CTRL+K+D to reformat the file currently opened: it will add all the missing this/me in the current file. See the option in Code Style-Formatting-General-Perform additional code cleanup...
    – Jimi
    Jan 14, 2020 at 17:11

1 Answer 1

6

Look for Tools/Options/Text Editor/C#/Code Style You will see a preference like;

Qualify property access with 'this' | Do not prefer 'this'

Change the option above and voila!

P.S: You still need to run code cleanup(ctrl+K+E) or set the severity to "Error", in which case VS itself will prompt to "fix" it as @stuartd mentions it in the comments

3
  • 4
    Well, you can set the severity to "Error", in which case VS itself will prompt to "fix" it.
    – stuartd
    Jan 14, 2020 at 17:01
  • 1
    If you set it to "Suggestion" the yellow bulb on the left side will appear and you will be able to apply this change at solution, project or document level.
    – Pepelui360
    Jan 14, 2020 at 17:09
  • Thank you so much. This works great. I cant seem to make it work with the code cleanup, but your answer is definitely correct. Jan 14, 2020 at 17:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.