How ToT2 Dark_1200x303

Code snippets are pre-written blocks of code we can save and then reuse in our code files—winning time while programming!

Howdy!! 🙋‍♀ This time we will be learning about code snippets. As programmers, we are always finding techniques and tools that allow us to save time and reuse code in order to make our work more efficient. That’s why I really like code snippets—because they let us win time while programming. Code snippets (sometimes named code templates) are pre-written blocks of code that can be saved so we can later reuse them in our code files. We can create these code blocks thanks to this Visual Studio functionality, which is available both for Windows and for Mac.

We will be dividing this post into the following topics:

✔ Creating code snippet template
✔ How to insert one

So, Let's Start! 😎

⭐ Creating Code Snippet Template

The main idea is to create a code block that we often need to repeat in our code. To have this code available as a code snippet, we have to create a code snippet template. So, next let’s learn how to do it:

  1. Open Visual Studio
  2. Go to Preferences: Once opened, a panel will be displayed.
  3. Text Editor: On the left panel you will see a toggle named this way—click it to show its options
  4. Click on Code Snippets option

Once displayed you will see an image like the one above. As you can see, we have some snippets created by default for some languages such as C#. (We will see how to insert / consume these ones that are already created.)

To create one, you just have to click the “Add” button and you will be able to see all the fields needed:

  • Shortcut: This is the shortname to get access to the snippet

  • Group: Here you can add the language group—it could be C#, HTML, Python, Razor or F#

  • Description: Write a short review of what your code does

  • Mime: It can be for text/x-csharp, text/html, text/x-python, text/x-cshtml or text/x-fsharp

And your code snippet is done! 😎

⭐ How to Insert One

🕵‍♂ Tab Expansion: You just have to start writing your code snippet name and press Tab two times, and your code will automatically be added to your class. (With this method, you can insert both those created by you and those that came created by default that we saw above.)

And that’s all! I hope all this information can be useful to you! 💚

Thanks for reading!

About the Author

Leomaris Reyes

Leomaris Reyes is a Software Engineer from the Dominican Republic, with more than 5 years of experience. A Xamarin Certified Mobile Developer, she is also the founder of  Stemelle, an entity that works with software developers, training and mentoring with a main goal of including women in Tech. Leomaris really loves learning new things! 💚💕 You can follow her: Twitter, LinkedIn , AskXammy and Medium.

Related Posts


Comments are disabled in preview mode.