"prop" snippet

Hey Everyone,

Before installing ReSharper I could create a property quickly and easily: prop TAB TAB and then you simply supply the type and name, with ReSharper it creates this:

public TYPE TYPE { get; set; }

How is that useful? I have to go in there and edit the get and set manually?

2 comments

This is a code snippet utilizing a new feature for C# 3.0 called "Auto-Implemented Properties." (see http://msdn.microsoft.com/en-us/library/bb384054.aspx) Basically, when the compiler sees a declaration like:
int MyProperty { get; set; }
It generates an invisible backing member variable and appropriate getter and setter methods to get and set the value for that variable. So, if all you want is a simple property with a getter and setter, there is no need to edit this code any further. If you need something more than that, you will have to change the code to add bodies to the getter and setter. Note that you will have to add bodies to both--you can't have a custom getter and an automatic setter, or vice versa.

The reason you are getting this code snippet now when you weren't before is because both R# and VStudio have a snippet with the same name, and R# gets first dibs on expanding it. If you find you are wanting properties with custom getters and setters all the time, you can create a different snippet in R#.

Hope this helps,
..David..

0

Hello,

If you need something
more than that, you will have to change the code to add bodies to the
getter and setter.


There's a context action (yellow lightbulb) on the auto-property name to
quickly convert it into a regular property with a getter and a setter.

Also, if you already have a field and would like to expose it through a property,
you might want to use the Generate (Alt+Ins) functionality, or the Incapsulate
Field refactoring.


Serge Baltic
JetBrains, Inc — http://www.jetbrains.com
“Develop with pleasure!”


0

Please sign in to leave a comment.