VS2005 ‘prop’ C# Code Snippet in VS2008

Automatic properties are nice, but sometimes you just want a good ol’ regular property with a backing field. Visual C# 2005 had the ‘prop’ code snippet which generated this:

visual c# 2005 prop code snippet

Visual C# 2008 now generates the following when using the ‘prop’ snippet:

2008-02-26_2330_001

I find myself wanting to use the old snippet more than the new one, so I went into the 2005 code snippets, copied the ‘prop’ snippet file, made a few changes and imported it into 2008 (Tools | Code Snippets Manager | Import).

Now, all is well:

Here’s a .zip file of the snippet that you can download.

Posted February 27th, 2008 1:16 AM
Read more posts about .NET, Tips.

Comments
Link

  • Thanks :-)
  • thank you for this!
  • Hello, I just want to thank you for the code snippet.

    I find the new way is breaking my code routine, because it's hard to make a property that's readonly.
  • Mizan
    You can do it by setting an attribute

    [DefaultValue(false)]
    public bool SomeProperty
    {
    get;
    set;
    }
  • jason b
    Not really on topic, but I love the contrast settings you have in that screen shot...the background is black with some of the key words in orange and so forth. Can you export your settings to a file or make it available here? By the end of the day my eyes are dead tired from staring at white screens. I've tried playing with the settings, but I'm graphically challenged I guess you could say.
  • John
    Best I can tell from the C# Code Snippet docs, you cannot transform the backing field name into the property name (or vice versa). There's very basic support for functions, and none of them cover this case.
  • Why do you have to type the name of backing field and property? Surely it is possible to have one derived from another?
  • As A comment to the post, I add that the old prop snippet is a must when you are not yet using the new Framework but still use Framework 2.0 as I do.

    And I ask you a hint if you can:

    I dont' know if it's a problem in my installation of Visual Studio But today I used for the first time a snippet and When I pressed tab instead of going from the first placeholder to the second it simply inserted a tab in the placeholder variabile
    such it happened in Visual Basic 2005.
    Have you ever had such behaviour? Could it be generated from some crazy default setting?

    Sabrina
  • Nice one!

    For your information I submitted this bug on Connect some time ago:
    http://weblogs.asp.net/lduveau/archive/2008/03/...
  • nice going mate!
  • John
    For default values. For instance, if I have a string property that should always have a default, I can't do that with automatic props.
  • Aaron Powell
    Umm... why? The C# 3.0 compiler will generate valid IL from the new prop. The old one is no longer needed as it adds unnessisary lines of code.
blog comments powered by Disqus