Reader small image

You're reading from  Hands-On Visual Studio 2022 - Second Edition

Product typeBook
Published inMar 2024
PublisherPackt
ISBN-139781835080443
Edition2nd Edition
Right arrow
Authors (2):
Hector Uriel Perez Rojas
Hector Uriel Perez Rojas
author image
Hector Uriel Perez Rojas

Hector is an experienced senior developer, with more than 10 years of experience in developing desktop, web, and mobile solutions with the .NET platform. He was recognized with the Microsoft MVP award for two consecutive years in 2021 and 2022. He is an active member of the .NET community and is the founder of El Camino Dev and Devs School academies.
Read more about Hector Uriel Perez Rojas

Miguel Angel Teheran Garcia
Miguel Angel Teheran Garcia
author image
Miguel Angel Teheran Garcia

Miguel Angel Teheran Garcia is a solutions Architect and Technical Lead recognized as a Microsoft MVP, C# corner MVP, and Alibaba Cloud MVP. He is a member of the Avanet community in Medellín and an active member of tech events in Colombia. He has also been a speaker at different conferences around Latin America.
Read more about Miguel Angel Teheran Garcia

View More author details
Right arrow

Adding Code Snippets

Coding is a lovely activity, but at times, some repeated statements are used to solve a few already known situations, which makes coding a more monotonous process. Code snippets represent a good resource for reusing pieces of code where desirable. By default, VS has some code snippets that we can use while we are coding, although it is also possible to manage them within VS, either to add new code snippets or to remove them.

In this chapter, we will learn about how VS helps us to write code faster by using code snippets and how to create our own versions.

We will review the following topics and functionalities for code snippets:

  • What are code snippets?
  • Creating code snippets
  • Deleting code snippets
  • Importing code snippets

Let’s start recognizing the concept of code snippets and how they work in VS.

Technical requirements

In order to complete the demos in this book chapter, you must have previously installed VS 2022 along with the web development workload, as shown in Chapter 1, Getting Started with Visual Studio 2022. You can download the project used in this chapter at the following link: https://github.com/PacktPublishing/Hands-On-Visual-Studio-2022-Second-Edition/tree/main/Chapter%204.

What are code snippets?

Code snippets are a simple and easy way to reuse code by creating templates that generate common statements, such as conditionals, loops, or comment structures.

VS has many code snippets by default for almost all the supported technologies and programming languages.

Important note

Code snippets are a common concept in software development. Almost all IDEs and code editors provide code snippets or have extensions to include code snippets.

In order for you to follow the different case studies used throughout this chapter, you can create a new project by using the ASP.NET Core with React.js template or downloading the resources mentioned in the Technical requirements section, where you will find a folder called Start with the initial files of the project.

Once you have the project open in VS, you will need to create a new condition to return an empty collection using the Get method by navigating to the WeatherForecastController.cs file. Just write...

Creating code snippets

In order to create a code snippet in VS, we need to create a file with the snippet extension. This file has an XML format, and there is a base template that we can update to include the information for our code snippet. The following code is a template example:

<?xml version="1.0" encoding="utf-8"?><CodeSnippets xmlns="http://schemas.microsoft.com/
    VisualStudio/2005/CodeSnippet">
    <CodeSnippet Format="1.0.0">
        <Header>
            <Title></Title>
            <Author></Author>
            <Description></Description>
            <...

Deleting code snippets

Due to human error, we can add code snippets that we don’t need, or maybe we can select the wrong code snippet. For these scenarios, VS has an option to delete code snippets. In order to see this option, navigate to Tools | Code Snippets Manager and select the CodeSnippets folder. This folder contains the code snippet that you included in the Creating code snippets section. If you used a different name, select the correct folder for you. You can see the Remove button location in Figure 4.12:

Figure 4.12 – The Remove button in Code Snippets Manager

Figure 4.12 – The Remove button in Code Snippets Manager

The Remove button will delete the whole folder, including all the code snippets inside. In VS 2022, it’s not possible to remove code snippets one by one, and therefore, we need to create a folder with a proper name for our code snippets. After removing the folder, VS will not suggest the code snippets anymore.

Important note

When you remove code snippets in VS...

Importing code snippets

If we want to include code snippets in a folder already created in the Code Snippets Manager, we can use the Import… option:

Figure 4.13 – The "Import…" button in Code Snippets Manager

Figure 4.13 – The "Import…" button in Code Snippets Manager

After clicking on Import…, you need to select the code snippet that you want to import in the selected folder. There is a filter related to the .snippet extension in the modal (see Figure 4.14):

Figure 4.14 – Selecting a code snippet file (.snippet) in the filesystem

Figure 4.14 – Selecting a code snippet file (.snippet) in the filesystem

Select the code snippet created in the Creating code snippets section, and then click on Open. Finally, you must complete the import process by selecting the location folder for your code snippet and clicking on Finish:

Figure 4.15 – The selection location for the imported code snippet

Figure 4.15 – The selection location for the imported code snippet

After completing the importation, you will see that the code snippet is added to the selected...

Summary

Now, you can use code snippets in VS and increase your productivity. You can identify which pieces of code are common in your architecture and use patterns to create your own code snippets to meet your requirements. Also, you know how to manage code snippets by using the functionalities to delete and import. After completing the demos in this chapter, you will recognize the importance of code snippets and why VS is a powerful IDE that helps developers write code faster.

In Chapter 5, Coding Efficiently with AI and Code Views, we will review the artificial intelligence (AI) tools included with VS and the ones we can add to the IDE, and we will learn how these tools can help us to write code faster and improve the syntax in some scenarios. You will also carry out some demos where AI will help you, allowing you to predict what action or statement you want to perform. So stay tuned!

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Hands-On Visual Studio 2022 - Second Edition
Published in: Mar 2024Publisher: PacktISBN-13: 9781835080443
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Authors (2)

author image
Hector Uriel Perez Rojas

Hector is an experienced senior developer, with more than 10 years of experience in developing desktop, web, and mobile solutions with the .NET platform. He was recognized with the Microsoft MVP award for two consecutive years in 2021 and 2022. He is an active member of the .NET community and is the founder of El Camino Dev and Devs School academies.
Read more about Hector Uriel Perez Rojas

author image
Miguel Angel Teheran Garcia

Miguel Angel Teheran Garcia is a solutions Architect and Technical Lead recognized as a Microsoft MVP, C# corner MVP, and Alibaba Cloud MVP. He is a member of the Avanet community in Medellín and an active member of tech events in Colombia. He has also been a speaker at different conferences around Latin America.
Read more about Miguel Angel Teheran Garcia