Development tip - Try using Visual Studio Code Snippets

If you haven't been programming in visual studio for long, chances are you haven't heard about code snippets.

Code snippet is actually code which you can quickly insert into the code editor. Typically snippets are common language constructs which you use often and a number of them come bundled with VIsual Studio. You probably noticed them in IntelliSense as you were typing code.

As the tooltip says, you can simply go to code snippet and then hit TAB key twice to insert it.

Some useful C# snippets are:

  1. #region - Inserts #region and #endregion, puts a cursor where you would normally type the name.
  2. try - Inserts a try-catch
  3. tryf - Inserts a try-finally
  4. for and forr - Insert for and reverse for loops
  5. prop - Inserts an automatic property
  6. propfull - Inserts a property and a backing field

As snippets for these short constructs exist, there are also snippets for entire classes and methods. And you can also create your own. To see all snippets that are shipped with Visual Studio, simply go to Tools -> Code Snippets Manager...

This nifty little thing is also available in Visual Studio Community Edition. You can play with the snippets, download new ones from the Internet and if you try to get into a habit of using them, possibly even improve your coding practices.

In a future blog post we'll create a small code snippet of our own.

Happy coding!

Comments are closed