What is a Code Snippet

A code snippet is a small chunk of reusable code which is used in development and inserted in the code file using the context menu or some combination of the hotkeys and a specified keyword.

Example
If-else block tries to catch or auto implement it property, we add it through the context, as well as some combination of hotkeys or the keywords.

There are 2 types of snippets
  1. Expansion Snippet
    This is added at the particular insertion point. For instance, if we want to add the if block, we type if and press the tab so the if keyword is expanded to the if(true){ } block, then this is inserted by the keyword if and by using the context menu.

    Example

    Type if and press tab and you get the if block; if you type tryf we get the try { } finally {} ; and if we type class and press tab we get the class declaration

  2. Surrounded-With Snippet
    It surrounds the specific chunk of code “like in array index print”, we surrounded it with the for loop, which surrounded the snippet as we type.

    Return x; select the return x and then right click. Click on the surrounded snippet and then pick the desired snippet.

Step 1

Step 2

  1. SqlConnection Con=newSqlConnection("XYZ");
  2. SqlCommand cmd=newSqlCommand("XYZ",Con);
  3. cmd.Parameters.AddWithValue("XYZ", 2.ToString()); //atleast for 1 param
  4. Con.Open();
  5. cmd.ExecuteNonQuery();
  6. Con.Close();

All the work is done and it's the time to check the snippet.

Open any VS Project type “SQL”, press tab two times, and you will get the snippet.



Press the tab two times.



Conclusions

This is the complete tutorial to make the code snippet in Visual Studio. The code snippet rapidly enhances the productivity of the developer. I am enjoying the valuable feature of Visual Studio.