Visual Studio Tip: Use Code Snippets
There is a little used function of Visual Studio that will save you a lot of coding time: Code Snippets. Code Snippets are handy key shortcuts that expand into commonly used .NET constructs such as regions, constructor, loops (do/while/for/foreach), and try/catch blocks. To use a code snippet you can type in its 'shortcut' and in Intellisense you will see a little piece of paper next to the word indicating that it is indeed a code snippet. Then you press TAB twice, and the code snippet is expanded into the full construct, and depending on the snippet you may be directed to enter some pertinent information.
Let's try my favorite example: a property declaration with a backing field.
- Type in prop and then press TAB twice
- You will see an expanded property declaration with green fields, indicating that you need to supply this information.
- The first field to fill in will be highlighted in blue, which in this case is 'int'. Let's type 'string' to make a string property, but any .NET type will work.
- Now hit TAB again to go to the next field, which is the name of the private variable. Let's call it _stringProperty.
- Press TAB again to go to the public property name. Notice that it filled in the get/set values of the private variable for you. Now you can call the public string StringProperty, and press ENTER to indicate that you are finished (if you press TAB, you will cycle back through the customizable fields).
Now this saves me a lot of time since I can create properties rapidly instead of typing curly braces and get/set over and over. Other huge time savers include foreach/for, try, and cw (which does a Console.WriteLine()).