Thursday, March 24, 2011

Visual Studio 2010 Tips and Tricks # 4 : Code Snippets

 

  • This is not a new feature, Code Snippets did exist in previous versions of Visual Studio.
  • Let us look at what are code snippets and how to use them.
  • Trust me they are very helpful for rapid application development. Ninja
  • Assume you are creating a class with member variables and basic getter, setter methods. Now you can sit and write the entire line for each member variable (or) you can make it rapid by following the steps shown below,
  • If you right click on your project and select “Add –> New Class –> enter class name”, it creates the class stub.
  • Now go in to the class and type “prop”

prop_1

 

  • A drop-down gets shown select “prop” and hit tab twice (tab-tab)
  • It will auto-fill the default datatype “int” and puts the “Get/Set” for you.

prop_2

  • Now if you want to change the datatype, click on “tab” and enter say “DateTime” on top of “int”.
  • And click another “tab” and type in your property name and that’s it you just wrote one member variable so fast Party smile

prop_3

 

  • Now having enjoyed rapid authoring, you may be wondering is there more auto-fill’s for most common syntax/structures.
  • Yes there is, for example, say you want to enter try catch. Just enter try and the dropdown shows available snippets,

prop_5

  • Select “try” and hit tab-tab

prop_4

  • Cool right you just saved yourself time entering the braces and aligning them etc..
  • Yes, so your next question is how do I know what all snippets are there, it is very easy,
  • Just right click on your code, select “Insert Snippet” or use short cut key “CTRL + K AND CTRL + X” to show the “Insert snippet” option.
  • Select the folder, say “Visual C#” and see all available snippet options, as shown below,

prop_6

  • Use lot of snippets while you are coding and increase your coding productivity. Nerd smile
  • Become a “Ninja Coder” Ninja
  • Stay tuned for my post about “how to create custom snippets”.

No comments: