Visual Studio 2008 Automatic Property is the default snippet

Visual Studio 2008 Automatic Property is the default snippet

  • Comments 18

Now if you type prop and hit "tab" twice, this will bring the Automatic property not the conventional property declaration.

This is my new finding.

Namoskar!!!

Leave a Comment
  • Please add 2 and 5 and type the answer here:
  • Post
  • PingBack from http://www.artofbam.com/wordpress/?p=5023

  • Is there a fast way to get the conventional property? I find this new implementation not handy at all!!!

  • Hi "A Student"

    I am not convinced. But if you wish to get the conventional property you can do so by adding your own snippet.

    Wriju

  • Ok, tell me then, why you are not convinced?

  • The Snippet generation for the conventional way has been replaced by Automatic Property. So the snippet. If you require more complex property then you have to write the code, where snippet may not be very helpful.

    Regards,

    Wriju

  • Hey Wriju,

    When I enter "prop" and hit tab  twice I get the following text entered in the editor by intellisense...

    "public int MyProperty { get; set; }"

    ...most of the documentation that I have been reading states that it should be writing out the private field as well as the property as so...

    "private int myProperty;

    public int MyProperty {

      get return myProperty;

      set value = myProperty; }"

    ...so I'm wondering if this is what you're referring to, or if not, might this be happening with you as well?

    Thanks,

    -Matt

  • Hi Matt,

    Yes you are right. "prop" now uses Automatic Property in VS 2008.

    The private field one was the feature with VS 2005.

    Thanks,

    Wriju

  • To be precise this new snippet now takes advantage of the "Auto-Implemented Properties" which is a feature of C# 3.0 where the backing field is automatically created by the compiler.

  • I call shenanigans on that. VS '08 is supposed to support multiple framework versions. C# 3.0 does not exist in .net 2.0,3.0.

    "Auto-Implemented Properties" is syntax candy at best. There's no reason to drop what is probably the most used snippet in C#.

    Put it back in please.

    -Geoff

  • I second Geoff's comments.

    I'll have to add my own - but it seems silly to replace one of the only snippets I found useful for something targeted at the 3.0 framework, and something which encourages lazy and bad practices. One of the helpful things about snippets is that they encourage good practices.

    If it ain't broke - don't fix it.

  • Yep. I just copied the 2005 dir into my 2008 dir. It is silly but I have to admit it's very satisfying to fix something that's been bugging me. Here's to the small victories eh?

    -Geoff

  • Look people, simply copy the text below and save it as "propc.snippet". this to your Visual Studio 2008 snippets folder. after you save this file there, you can type your shortcut "propc" and hit tab twice and the conventional property will populate.

    My path looks like this:

    C:\Users\ShYnE\Documents\Visual Studio 2008\Code Snippets\Visual C#\My Code Snippets

    <?xml version="1.0" encoding="utf-8" ?>

    <CodeSnippets  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">

    <CodeSnippet Format="1.0.0">

    <Header>

    <Title>prop</Title>

    <Shortcut>prop</Shortcut>

    <Description>Code snippet for property and backing field</Description>

    <Author>Microsoft Corporation</Author>

    <SnippetTypes>

    <SnippetType>Expansion</SnippetType>

    </SnippetTypes>

    </Header>

    <Snippet>

    <Declarations>

    <Literal>

    <ID>type</ID>

    <ToolTip>Property type</ToolTip>

    <Default>int</Default>

    </Literal>

    <Literal>

    <ID>property</ID>

    <ToolTip>Property name</ToolTip>

    <Default>MyProperty</Default>

    </Literal>

    <Literal>

    <ID>field</ID>

    <ToolTip>The variable backing this property</ToolTip>

    <Default>myVar</Default>

    </Literal>

    </Declarations>

    <Code Language="csharp"><![CDATA[private $type$ $field$;

    public $type$ $property$

    {

    get { return $field$;}

    set { $field$ = value;}

    }

    $end$]]>

    </Code>

    </Snippet>

    </CodeSnippet>

    </CodeSnippets>

  • Little typo I guess

    Please replace <Shortcut>prop</Shortcut> with

    <Shortcut>propc</Shortcut> to get the shortcut.

    Thanks for posting it.

    Wriju

  • I have to agree/disagree with the change.

    At first, I was surprised by the snippet (since I was targeting .net 2.0 and I was forced to manually expand all the properties).

    However, then I realized that the automatic properties are compiled out and the dll is a .net 2.0 dll. Then, I started to realize many other cool C# 3.0 features work when targeting .net 2.0.  

    So in the end, I say thank you MS for making me realize that the automatic prop is the right way to do it even when targeting .net 2.0. (For simple properties).

    Now the complaint:

    Sometimes, I need logic in my get or set. Often this is added at a later point after the automatic property already exists.

    C# 3.0 needs an automatic -> conventional property refactor option. This would be awesome and a great time saver for me.

  • > C# 3.0 needs an automatic -> conventional property refactor option. This would be awesome and a great time saver for me.

    Check out resharper - http://www.jetbrains.com/resharper/ it does exactly this.

Page 1 of 2 (18 items) 12