Visual Studio – How to make the Implement Interface quick action generate auto properties

When you use the Implement Interface quick action in Visual Studio, it generates all members that throw exceptions, even the property getters and setters:

public class Coder : IPerson { public string FirstName { get => throw new NotImplementedException(); set => throw new NotImplementedException(); } public string LastName { get => throw new NotImplementedException(); set => throw new NotImplementedException(); } public void DoWork() { throw new NotImplementedException(); } }
Code language: C# (cs)

This is fine for methods, but not for properties. You’d expect it to generate auto properties. Instead, it generates getters/setters that you then have to go clean up.

Fortunately, this behavior is controlled by a setting in Visual Studio. You can change it to generate auto properties instead (without changing how it generates methods). Here’s how:

  • In the Visual Studio menu, click Tools > Options
  • In the options, navigate to Text Editor > C# > Advanced
  • Scroll down and find the Implement Interface or Abstract Class section.
  • Select prefer auto properties.
  • Click OK.
Visual Studio options window showing how to select the "prefer auto properties" for the Implement Interface quick action.

Now when you use the Implement Interface quick action, it’ll generate auto properties:

public class Coder : IPerson { public string FirstName { get; set; } public string LastName { get; set; } public void DoWork() { throw new NotImplementedException(); } }
Code language: C# (cs)

Notice how the generated method still throws an exception. As mentioned, changing this setting doesn’t change how it generates method. At the end, you have good default behavior for both methods and properties.

Leave a Comment