How to generate getters and setters in Visual Studio?


By "generate", I mean auto-generation of the code necessary for a particuliar selected (set of) variable(s).

But any more explicit explication or comment on good practice is welcome.

10/4/2012 7:54:36 AM

Accepted Answer

Rather than using ctrl+k,x you can also just type prop and then hit tab twice

2/6/2013 6:04:37 AM

Visual Studio also has a feature that will generate a Property from a private variable.

If you right-click on a variable, in the context menu that pops up click on the "Refactor" item. Then choose encapsulate field. This will create a getter/setter property for a variable.

I'm not too big a fan of this technique as it is a little bit awkward to use if you have to create a lot of getters/setters, and it puts the property directly below the private field, which bugs me because I usually have all of my private fields grouped together, and this Visual Studio feature breaks my class' formatting.

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow