Snippet: Property Snippets

When you write custom classes in C# as often as I do, you begin to get somewhat sick of having to type out backing fields and property getters/setters for a whole load of properties in a class. I’ve used a number of tools and shortcuts to make this easy, such as the “prop” snippet in Visual Studio 2005, the Encapsulate Field context menu item, or the quick shortcuts available in CodeRush, but I usually find that the time I save in using these tools is lost in having to rearrange the code they generate.

Today, whilst teaching our Pro .NET course in Sydney, I was thinking about this very problem and about the way I write my classes.

See, I generally write all of my private fields first (at the top of the class), then I write all of my properties afterwards. I tend to write one field, then copy and paste it a few times underneath, then go in and edit the copies to suit. I’ll then do the same with properties - write the first, then copy and paste that a few times and then edit the copies to match them to the fields. Usually I’ll copy and paste more than I need, but it’s always easier to delete a line than to write a new one (such is the fickle nature of code :)).

Another observation I made was that the times when I need some shortcuts to create these properties are usually when I have a lot of properties to create, rather than just one.

So tonight I came up with some C# code snippets to solve this problem, and I’ve attached them to this blog entry.

The first is called “fields“. In typing the word “fields” and pressing tab key twice, it generates a list of 5 fields. I can then enter the type of the field, hit tab, then enter the name of the field, hit tab, and repeat over and over. If it turns out that I need less than 5 fields, it’s faster to delete the extra lines than to write them. This is how it looks:

The second is called “props“. When I type “props” and hit the tab key twice, it generates 5 property getters and setters, formatted the way I like them. Again, I just tab through the fields in green until I hit the end. This is how it looks:

The last is called “propsdb“. This is a special snippet I’ve made for when I’m writing classes that use a lot of data binding. It’s similar to props, except it also generates 5 cached PropertyChangedEventArgs objects (mentioned in my last screencast) and it adds code to raise PropertyChanged events to each property. This is how she looks:

You can download the snippets here:

Note that you’ll probably want to change the formatting of the code to suit your style - if so, you can just open the .snippet files in Notepad and scroll towards the bottom. To install them, just unzip the .snippet files to either of the two locations below:

  • C:\Program Files\Microsoft Visual Studio 8\VC#\Snippets\1033\Visual C# for everyone who uses the PC (note that on Vista you’ll need to have Admin privelliges to write to this location).
  • <My Documents>\Visual Studio 2005\Code Snippets\Visual C#\My Code Snippets just for you.

I hope you find them useful!

10 Responses to “Snippet: Property Snippets”

  1. Hi,

    Have you tried vsPropertyGenerator2? It’s a tool I use all the time. I do also put the private fields on top in a class and then vsPropertyGenerator lets you choose where to put the properties (after last field, last in class etc.)

    Get it here: http://www.adersoftware.com/index.cfm?page=vsPropertyGenerator2

  2. Or tried ReSharper? R# for VS 2005 will create a property with backing field.

  3. Paul, Try this in coderush.

    type fs to create string field then type firstName then fs to create next string field and type lastName

    Then copy these 2 private fields into the clipboard and paste just below.

    Boom instant get/sets for all the fields in the clipboard matching.

  4. P.S You could also create a new custom template in coderush to do the same thing for PropertyChangedEventArgs so you just do something like fpc and it generates the private field then on paste generates the get/set as well :)

    It beats cutting out 3 get/sets because you only wanted 2 really in your snippet.

    You can also to say rp for Read Only Property which includes the private field as well.

    Code Rush rocks :)

  5. Hi Justin,

    Yeah, but then you have to pay for Code Rush :)

    wwfDev,

    Thanks for the link, I’ll try it.

    Thomas,

    I have used Resharper but I liked CodeRush better - the cyclomatic complexity counter that appears next to method names was the real draw. Last time I tried Resharper 2005 I found it a little slow and it generated lots of folders under the project structure which was ugly.

  6. > Yeah, but then you have to pay for Code Rush

    But of course your time is worth way more than a couple hunjie and if it saves you 10 minutes a day then it will pay for itself over the year.

    Don’t reinvent the wheel just add a supercharger to it and keep on movin.

  7. […] PaulStovell.NET » Property Snippets (tags: c# programming visualstudio) […]

  8. Great snippet!

    Even when some people may say you could by a tool
    for doing this (which is correct), sometimes you just don’t want to spend money on this kind of small solutions you could do by your self and then share. I like your vision better.

    Keep on like that man!

  9. Personally I’d change that property set method so that it does a compare before firing off the event otherwise you’re signalling a change when one hasn’t actually occured.

    i.e.

    public string Property
    {
    set { if (_field != value) {
    _field = value;
    OnPropertyChanged(…);
    }
    }

    [)amien

  10. Hi, dunno if this is the right place to ask.. but perhaps someone can shove me in the right direction..?

    I’d like to change the code written by the auto complete in VS 2005. Example: in VB type: Public Property someProp as Integer
    and then hit enter. VS auto-completes the property signature by adding get/set accessors. I’d like to change the code that VS generates. Is this possible? Ideas on how to do this?

    Thanks,
    -Boo

Leave a Reply