If you have ever typed prop and hit tab twice in Visual Studio 2005 or 2008, you have used a snippet even if you do not know a snippet is.  In VS2005 I used the prop snippet all the time to create a field and property automatically for me.  The snippet goodness continued in Visual Studio 2008, except...

With the new property definition

public bool IsVaild{ get; set; }

There was no longer a need for a field so the snippet was changed and it no longer creates one.  Well, that is great since 90% of the properties written just set the field to the value and return it... right?

Enter Lazy loading, INotifyPropertyChanged and verification of data.  As soon as you need to do something in the get or set you have to have a field.  And that really is not a large issue until you think about how many properties are scattered in your project.  INotifyPropertyChanged raises an event when a property is changed, it does this by calling a method in the properties set accessor.  If I have an Int32 property but I do not want values over 42, I need the set accessor to check the new value and throw an exception if the value is greater than 42.  An easy way to use lazy loading is to check the field for a null value in the get accessor and load the database on the first request when the property is null.  Now I would rather always have my field, and accessors, declared so that if I happen to need them, they are already there.  Yes, yes I know 90% of the time you do not need them.  Or do you? 

I am happy to say you will rarely need to limit the value of an Int32 property and that most applications do not need an event fired whenever a property is changed.  However, in a large application what kind of performance gain is there in lazy loading?  If I load a trouble ticket, and that ticket has the person who submitted it, the representative who work on it and each person has a list of tickets they have submitted or worked on... well that is a great deal of data to load.  But if the person who submitted the ticket and the representative who worked on it are only loaded when they are requested, they may never be loaded and you save the over head of loading them and any child objects they might have in them.

So where are the snippets?

C:\Program Files\Microsoft Visual Studio 9.0\VC#\Snippets\1033\Visual C#

if you want the field back, find the prop.snippet file and open it in Visual studio or notepad.   Here is the content for the file...

<?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 an automatically implemented property</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>

There are many useful snippets in VS2008 by default, you can create your own, download other peoples snippets, modify/use the default ones or ignore them altogether.

Here are some of the snippets I use frequently

  • lock
  • if
  • forr
  • foreach
  • for
  • propg
  • switch, when using an enum this is hand, but I avoid enums anyways. 
  • try
  • tryf
  • using
  • while
  • ctor
  • else
  • exception

Write your code so you are happy with it; not only that it works, but how it reads and how you can maintain it.  Do not write it because that is the way the IDE set it up.  If you want to know more about snippets, check out http://msdn.microsoft.com/en-us/library/ms165392.aspx