Partilhar via


Visual Studio 2008 Automatic Property is the default snippet

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!!!

Comments

  • Anonymous
    October 03, 2007
    PingBack from http://www.artofbam.com/wordpress/?p=5023

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

  • Anonymous
    October 10, 2007
    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

  • Anonymous
    October 11, 2007
    Ok, tell me then, why you are not convinced?

  • Anonymous
    October 15, 2007
    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

  • Anonymous
    November 05, 2007
    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

  • Anonymous
    November 05, 2007
    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

  • Anonymous
    November 07, 2007
    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.

  • Anonymous
    November 29, 2007
    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

  • Anonymous
    December 04, 2007
    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.

  • Anonymous
    December 12, 2007
    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

  • Anonymous
    January 14, 2008
    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:UsersShYnEDocumentsVisual Studio 2008Code SnippetsVisual 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>

  • Anonymous
    January 16, 2008
    Little typo I guess Please replace <Shortcut>prop</Shortcut> with <Shortcut>propc</Shortcut> to get the shortcut. Thanks for posting it. Wriju

  • Anonymous
    January 30, 2008
    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.

  • Anonymous
    June 24, 2008
    > 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.

  • Anonymous
    August 15, 2008
    Thanks for the info here. I've modified Shyne's code a bit to suite my own needs, but if its helpful to anyone, here goes: I created two prop snippets - one for the regular prop and one for nullable types, that I use often. For the regular types (propc), I got: <![CDATA[ private $type$ _$field$ = $defVal$; [$xmlType$] public $type$ $field$ {   get { return _$field$;}   set { _$field$ = value;} } $end$]]> and for nullable types (propn): <![CDATA[ private $type$? $field$ = $defVal$; [$xmlType$] public $type$? $field$ {   get { if($field$.HasValue) return _$field$; else return null;}   set { if(value.HasValue) _$field$ = value.Value; else _$field$ = null;} } $end$]]> On both, I use the same name for the field and property, but prefix the field name with an underscore; I've added to both an XMLSerialization attribute (need to add "using System.Xml.Serialization" to the classe) and added a default value to the attributes. I got the full code on my blog: http://miguelalho.com/?p=590 Hope it's usefull to someone...

  • Anonymous
    February 20, 2009
    They should have included a conventional property snippet in the final release, imo. Sure, it's easy enough to create your own snippet or copy someone elses, but it seems to me that conventional properties will still be needed and just seems like a no-brainer that it should be in there. For instance, when the property needs to be assigned a default value. The only way to do this (I think?) with auto props is in the constructor, which isn't as efficient because the prop will be assigned null by the compiler and then explicitly assigned in the constructor.

  • Anonymous
    June 18, 2009
    FAQTs - Knowledge Base - View Entry - Why parseInt(08) &amp; parseInt(09) is showing the value 0 ? (tags