Automatic properties and replacing the Visual Studio 2008 ‘prop’ snippet

In .NET Framework 3.5, we’ve gained automatic properties. Automatic properties are really simple to understand. Instead of writing a full getter and setter with a private field (called a backing field, because it belongs to the property) you just write your property like this.

namespace ClassA.Examples.AutomaticProperties

{

  public class Customer

  {

    public string FirstName { get; set; }

    public string LastName { get; set; }

    public string City { get; set; }

  }

}

The compiler creates a ‘real’ property with a backing field, which you can view with Reflector or ildasm.

automaticproperties

If you want more info, check out this blog entry by Bart de Smet.

Why I’m writing this, is because of the property snippet. I used to use the property snippet a lot, which produces some handy templated code. Visual Studio 2008 supports multi targeting and you can write software focused on the .NET Framework 2.0, 3.0 or 3.5. So you can develop an application without ever touching .NET Framework 3.5 or C# 3.0. Until you want to create properties… The new property snippet is based on the automatic properties in C# 3.0.

To solve this problem, first change the name of your original snippet from prop.snippet to aprop.snippet, where aprop stands for “automatic property”. You can find the property in the following folder, if installed at the default location.

snippets_location

Just changing the filename isn’t it though. Below is the copy of the original Visual Studio 2005 property snippet. Notice the Title and ShortCut elements. In your aprop.snippet you should change this accordingly. Then copy the code below and create a new prop.snippet in the same folder. That’s it.

<?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 VS2005 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>

 

UPDATE : That’s really funny, while writing this article, Daniel Moth was doing exactly the same… Well, not this one, but one a bit shorter. That’s why my article was posted half an hour later! 😉

You may also like...

5 Responses

  1. abhishek says:

    Is automatic property assign default values while initialing property .In old property we can do it by setting default value to private value

  2. Laughing John says:

    I’ve created an MS connect request to have the old snippet reinstated and also to add initialization to automatic properties. Vote/comment if you’re interested!

    http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=361647

  3. Tapan says:

    I have vs 2005 and I am updating to vs 2008 so how to update my defalt proerties.

    thanks

  4. Dennis van der Stelt says:

    @Tapan : You don’t really have to… The new automatic properties are translated by the compiler into normal properties, as you wrote them in VS2005.

    You’ll still write properties the old way when you want to add behavior to them. But if you just have a getter and setter which get and set the _whatever value (the backing-field) you can use the new automatic properties.

    In other words, don’t change anything, it’ll all keep working. Just use the new properties from now on in new code.

  5. Aleksey says:

    Thank you for an advice. I’ve just started to use VS2010 so I still do not know tricks liks these ones.

Click on a tab to select how you'd like to leave your comment

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.