13 Visual Studio 2022 Tips & Tricks

13 Visual Studio 2022 Tips & Tricks


Many .NET developers use Visual Studio as their Integrated Development Environment (IDE). Visual Studio has many features designed to simplify developers’ lives and be more productive.

In this article, I want to show 13 tips and tricks to improve your developer experience.

Play

Depending on your experience as a .NET developer, you might know and use some of the tips in this video. However, I’m confident that you’ll learn at least something new.

1. Paste JSON as Classes

When working with APIs we often send a request and receive JSON data. Using C#, we need to transform the data into an object tree. That’s where what I’m going to show you is a game-changer.

First, let’s assume we receive the following JSON data from an API:

{
  "squadName": "Super hero squad",
  "homeTown": "Metro City",
  "formed": 2016,
  "secretBase": "Super tower",
  "active": true,
  "members": [
    {
      "name": "Molecule Man",
      "age": 29,
      "secretIdentity": "Dan Jukes",
      "powers": [
        "Radiation resistance",
        "Turning tiny",
        "Radiation blast"
      ]
    },
    {
      "name": "Madame Uppercut",
      "age": 39,
      "secretIdentity": "Jane Wilson",
      "powers": [
        "Million tonne punch",
        "Damage resistance",
        "Superhuman reflexes"
      ]
    },
    {
      "name": "Eternal Flame",
      "age": 1000000,
      "secretIdentity": "Unknown",
      "powers": [
        "Immortality",
        "Heat Immunity",
        "Inferno",
        "Teleportation",
        "Interdimensional travel"
      ]
    }
  ]
}

Now, let’s copy the JSON data into the clipboard, and in the Edit menu, select the Paste special menu and click on Paste JSON as classes.

public class Rootobject
{
    public string SquadName { get; set; }
    public string HomeTown { get; set; }
    public int Formed { get; set; }
    public string SecretBase { get; set; }
    public bool Active { get; set; }
    public Member[] Members { get; set; }
}

public class Member
{
    public string Name { get; set; }
    public int Age { get; set; }
    public string SecretIdentity { get; set; }
    public string[] Powers { get; set; }
}

As you can see, Visual Studio generated a class structure matching the JSON data. Imagine how much time and tedious typing work you’ll save if you have a big data structure. Visual Studio also can do the same for XML data.

2. Vertical Text Selection

You can use SHIFT + ALT and use the arrow keys to vertically select text. Let’s assume you have a few lines and want to apply the same changes. For example, you have a few lines of Console.WriteLine statements, and you want to use Console.Write instead.

Console.WriteLine("Hello, World!");
Console.WriteLine("Hello, World!");
Console.WriteLine("Hello, World!");
Console.WriteLine("Hello, World!");
Console.WriteLine("Hello, World!");

We press SHIFT + ALT and use the arrow up key to expanding the cursor to all lines we want to change. Next, we apply our changes. We can even move the cursor using the left and right arrow keys as you would with a normal cursor. If we want to leave the vertical text selection mode, we press the ESC key.

3. Bookmarks

If we want to mark a line for future edits, we can use comments and mark them using the same structure. For example, we can add a comment starting with TODO and a colon.

//TODO: Refactor before commit

Bookmarks are a better solution. Wherever we are, we can hit CTRL+K,K to bookmark a line in the solution. We can open the Bookmark window using the menu or use the CTRL+W,B keyboard shortcut. This window provides an overview of all bookmarks, and you can rename them using more descriptive text.

One thing to mention is that bookmarks are stored locally, which means they are not shared using source control. So if you want to share something with the team, comments within the source code are the better choice.

However, usually, bookmarks are used for short-term notes to yourself, and you avoid accidentally committing your personal notes into the source code repository.

4. Using Code Snippets

Code snippets are a powerful tool for everyday code writing. Most developers know about the ctor code snippet to create a constructor or the prop snippet to create a class property.

However, there are many more snippets that you can use to simplify your everyday tasks. I’ll quickly go through a few snippets I think are useful but not well known.

  • cw - Creates a Console.WriteLine statement.
  • try - Create a try-catch block
  • for - Creates a for loop with a predefined index variable incremented by 1.
  • forr - Creates a for loop with a predefined index variable decremented by 
  • exception - Creates a template for an exception implementation according to guidelines.

There are many more useful code snippets in Visual Studio. I challenge you to explore them yourself or create custom code snippets in the Code Snippets Manager specific to your projects.

5. Format Code

Code formatting improves readability and also helps to reduce code changes reflected in source control. The keyboard shortcut CTRL+K,D allows you to format the open document according to the rules set in the editor config file or the Visual Studio settings.

For example, this method definition can quickly turn into a well-formatted piece of code.

int Multiply( int a, int b )

{

       return a * b; }
int Multiply(int a, int b)
{
    return a * b;
}

6. Enable Code Cleanup on Save

This tip is almost an extension of tip 5. Enable Code Cleanup on Save. It used to be a feature of the well-known Productivity Power Tools Visual Studio extension. However, starting with Visual Studio 17.1, it is part of the core Visual Studio experience.

First, we open the Analyze menu and select the Configure Code Cleanup menu item in the Code Cleanup sub-menu.

There is a list of available fixes, and we can create profiles that include the code fixes we want to execute.

To enable Code Cleanup on Save, we open the Options dialog in the Tools menu. In the Text Editor section, we select Code Cleanup. We enable Code Cleanup on Save by checking the checkbox using one of the previously defined Code Cleanup profiles.

7. Cycling through Open Files

We often navigate from one file to another. Using the mouse is not only inconvenient but also slow. Visual Studio supports the CTRL+Tab, and CTRL+SHIFT+TAB hotkeys like browsers do. 

We can use those shortcuts to navigate through the open files within our solution. Visual Studio provides a window that lets us select the desired file. It is helpful when there are many files opened in the solution.

8. Bind Go to All to CTRL+T

One of my most used shortcuts in Visual Studio Code is CTRL+T which allows me to search for a file using part of its name. We can configure Visual Studio to allow us not only to search for files but also types.

To set it up, we open the Options dialog in the Tools menu. We then search for the Edit.GoToAll command, and assign the CTRL+T shortcut to it.

Back in the solution, we can use the CTRL+T shortcut to open a little text input which works the same as in Visual Studio Code.

9. Track Active Item in Solution

Working on bigger solutions can be challenging. There is an option to track the active item in the Solution Explorer. 

We open the Options menu again and select the Projects and Solutions item. We enable the Track Active Item in Solution Explorer to enable it.

Now, whenever I change the active file in the editor, the opened file is selected in the Solution Explorer.

10. Faster Startup

When working with bigger solutions we can experience long loading times. Visual Studio 2022 and even Visual Studio 2019 did a good job in reducing the solution and project loading times.

However, there are two opens we can enable to make it even faster.

We again open the Options menu and select Projects and Solutions. We disable the Reopen documents on solution load and disable Restore Solution Explorer project hierarchy state on solution load.

Both options are convenient, but most of the time, you don’t want to continue working where you left the solution anyway. If you have to deal with bigger solutions, try it out and let me know in the comments if and how much your solution loads faster.

11. Add Missing Using Directives on Paste

Whenever we copy and paste code, we need to add the missing using statements. We can enable an option to automatically add using statements.

Let’s open the Options dialog again and select Text Editor, C#, Advanced. We scroll down until we see the Using Directives section of the settings page. We enable the last option to Add missing using directives on paste

If you want, you can also enable the first option to place the System usings first when sorting usings.

12. Remove Unused References

If you work on a solution for a long time, chances are that you still have references to packages or projects that you don’t use in your code anymore.

Let’s, for example, install the Newtonsoft.JSON package in the NuGet Package Manager for our project.

In the Solution Explorer, we open the context menu of our project and select Remove unused references

Visual Studio analyses the solution and suggests what references to remove from the project. We can set the action for each reference and apply them all using a single button click.

13. Add Parameters to the Constructor

When working with data classes, we usually have a few properties, and we want to create a constructor sooner or later. We can do that using the Quick Fixes menu. We access it using the CTRL+. keyboard shortcut.

Let’s create a constructor including the Name property. Now, let’s do the same on the DateOfBirth property, but this time, we add the property to the existing constructor.

It’s a small thing but the time saved can add up over time. What we also can do using the Quick Fixes menu is generate ArgumentNull checks. We place the cursor in the constructor argument and open the Quick Fixes menu again. We select the desired option to generate the checks we need.

Don’t forget to subscribe to the YouTube channel to learn more about .NET development.

PREMIUM CONTENT
Get access to my C#/.NET Bootcamp Course and 100+ additional software development courses by industry-leading instructors on Zero To Mastery.
Get 10% OFF with the code: FRIENDS10