In our simple example here, we've taken the one property
Person class and overhauled it with the Person2 class, the entirety of which is
listed below as Listing 6.
private string _name;
public string Name
_name = value;
Clearly, there is a lot more code here than before and,
arguably, the code is a bit tougher to read with all this attribute
"clutter". We can fix the latter easily enough through the use of a
#region which can encapsulate the property in a named region (in this case
called Name). The end result is something we can easily collapse so that we
are not faced with all this clutter while we are working with our Person2
class. To do this, you could hand type #region Name and then #endregion, but a
much more efficient way is to take advantage of Code Snippets. In this case,
you can simply highlight the lines you would like to place in a #region and
right click, select Surround With, and then choose #region. The #region
statements are placed for you and the name of the region is highlighted in
green and has focus, such that as soon as you start typing it will have
whatever value you type for it. Hit enter and the Code Snippet wizard
completes, the green highlighting disappears, and you are left with your code.
Code Snippets are a fantastic feature of Visual Studio
2005. If you have not used them yet, make a point to try them out. There is
no limit to the kind of code generation you can accomplish with these
template-driven tools. You can learn
more about them, find others, and share your own at GotCodeSnippets.com.
For more on Code Snippets, please see my related article,
Visual Studio 2005 Code Snippets.