|
| 1 | +# 0801 - Visual Studio Tips and Tricks |
| 2 | + |
| 3 | +Since the beginning, Visual Studio has been the premiere tool to build and work with .NET code using C#. This windows-based integrated development environment tool has been available in various versions since 1997 to its latest version Visual Studio 2022. |
| 4 | + |
| 5 | +Wikipedia has a nice article about the history of [Visual Studio](https://en.wikipedia.org/wiki/Visual_Studio). |
| 6 | + |
| 7 | +## Current Version and Acquision |
| 8 | + |
| 9 | +[Visual Studio](https://visualstudio.com) is available in a freemium licensing model, with a [community edition](https://visualstudio.microsoft.com/vs/community/) available for students and folks that are hoobyists or learning. |
| 10 | + |
| 11 | +There is also a Professional edition available for organizations as well as an Enterprise edition with additional tools and access to services |
| 12 | + |
| 13 | +## Code Writing Tips |
| 14 | + |
| 15 | +- `Ctrl+.` to pop open the code suggestion panel with code-fixes and suggestions |
| 16 | +- `Ctrl+<SPACE>` to open the intellisense panel with code-completion suggestions |
| 17 | +- `<TAB>` to choose a code-completion option and accept it |
| 18 | +- `<UP>` and `<DOWN>` when the code-completion window is open to select other options |
| 19 | +- `<UP>` and `<DOWN>` when the method signature completion window is open to select optional override signatures |
| 20 | +- Hold the `Ctrl` key to make these pop-ups semi-transparent |
| 21 | + |
| 22 | +- `Ctrl+X` to cut highlighted text to the clipboard |
| 23 | +- `Ctrl+C` to copy highlighted text to the clipboard |
| 24 | +- `Ctrl+V` to paste the contents of the clipboard |
| 25 | +- `Ctrl+E C` or `Ctrl+K C` to comment the current line of code |
| 26 | +- `Ctrl+E U` or `Ctrl+K U` to uncomment the current line of code |
| 27 | + |
| 28 | +- `Alt+<UP>` to move the current line of code up |
| 29 | +- `Alt+<DOWN>` to move the current line of code down |
| 30 | + |
| 31 | +When writing HTML or other markup syntax |
| 32 | + |
| 33 | +- `Shift+Alt+W` to wrap the current markup with a div element |
| 34 | + |
| 35 | +When you have JSON or XML on the clipboard, try **Edit - Paste Special - Paste as Classes** |
| 36 | + |
| 37 | +Try the **C# Interactive** window to get a REPL experience where you can experiment writing code |
| 38 | + |
| 39 | +## Code Navigation Tips |
| 40 | + |
| 41 | +- `Ctrl+F` to find text in the current file |
| 42 | +- `Ctrl+H` to find and replace text in the current file |
| 43 | +- `Ctrl+Shift+F` to find text in the current solution / project with options including regular expressions |
| 44 | +- `Ctrl+Shift+H` to find and replace text in the current solution / project with options including regular expressions |
| 45 | +- `Ctrl+Q` - Search features of Visual Studio |
| 46 | +- `Ctrl+T` - Search your code - filenames, classes, interfaces, methods, properties, events, enums |
| 47 | +- `Ctrl+G` - Goto line number |
| 48 | +- `F12` - Goto definition of the object under cursor |
| 49 | +- `Shift+F12` - Find all references of the object under cursor |
| 50 | +- `Ctrl+F12` - Find all implementations of the interface under cursor, if not an interface, goes to definition |
| 51 | +- `Shift+F2` to add a new file with a template based on the extension you specify |
| 52 | +- `Previous Mouse Button` to navigate to the previous code ___location |
| 53 | +- `Next Mouse Button` to navigate to the next code ___location |
| 54 | +- `Ctrl+M M` to collapse / expand the current block of code |
| 55 | + |
| 56 | +Hold Alt while selecting lines vertically to select a block of code and work with all of it at once |
| 57 | + |
| 58 | +### Bookmarks |
| 59 | + |
| 60 | +- `Ctrl+B T` to toggle a bookmark for quick navigation |
| 61 | +- `Ctrl+B P` to navigate to the previous bookmark |
| 62 | +- `Ctrl+B N` to navigate to the next bookmark |
| 63 | + |
| 64 | +Customize the scroll bar to show a map of your file by right-clicking on the scroll-bar and choosing 'Scroll bar options' |
| 65 | + |
| 66 | +Right-click on the tab bar and choose **Set Tab Layout** to allow you to move the tabs to the left or right, and customize their appearance |
| 67 | + |
| 68 | +Configure Solution Explorer to **Track Active Item** to ensure that the current file is always highlighted in Solution Explorer |
| 69 | + |
| 70 | +## Debugger Tips |
| 71 | + |
| 72 | +- `F5` to start your application and begin debugging |
| 73 | +- `Ctrl+F5` to start your application WITHOUT debugging |
| 74 | +- `F10` to start your application, begin debugging, and stop on the first line of your code |
| 75 | + |
| 76 | +### While debugging |
| 77 | + |
| 78 | +- `F11` step into the next line of your code |
| 79 | +- `F10` step OVER the current code and don't execute it |
| 80 | +- `F9` toggle a breakpoint on the current line of code (can also be set when not debugging) |
| 81 | +- `Ctrl+Shift+F9` delete all breakpoints |
| 82 | +- `Shift+F11` execute and step out of the current method |
| 83 | +- `Strl+Alt+P` attach to currently running process |
| 84 | + |
| 85 | +Tricks to make debugging easier: |
| 86 | + |
| 87 | +- Right-click on a breakpoint and add a condition to force the breakpoint to only stop when a condition is met |
| 88 | +- Use Locals, Watch, and Immediate windows to get insight into the current variables in scope and execute arbitrary code while working in debug mode |
| 89 | +- Configure your own data for the locals and watch windows by adding the [`DebuggerDisplay` attribute](https://learn.microsoft.com/visualstudio/debugger/using-the-debuggerdisplay-attribute) |
| 90 | +- Use the Call Stack window to navigate back and forth across the calling methods |
| 91 | + |
| 92 | +More advanced debugging capabilities available: |
| 93 | + |
| 94 | +- Debug on a remote machine with the [remote debugger](https://learn.microsoft.com/visualstudio/debugger/remote-debugging). This requires installing some tools on the target machine |
| 95 | +- Debug inside a Docker container |
| 96 | +- Debug inside Windows Subsystem for Linux |
| 97 | +- Debug a [MAUI app on a connected device](https://learn.microsoft.com/dotnet/maui/android/device/setup) |
| 98 | +- Use the [Snapshot Debugger](https://learn.microsoft.com/azure/azure-monitor/snapshot-debugger/snapshot-debugger) to walk through a collected snapshot from Azure Application Insights |
| 99 | + |
| 100 | +## Make Visual Studio yours |
| 101 | + |
| 102 | +- Get your favorite themes from the [marketplace](https://marketplace.visualstudio.com/search?term=theme&target=VS&category=All%20categories&vsVersion=vs2022&sortBy=Relevance) |
| 103 | +- Convert themes from Visual Studio Code for Visual Studio 2022 with the [Theme Converter](https://github.com/microsoft/theme-converter-for-vs) |
| 104 | +- Play sounds for various Visual Studio events like 'Build Succeeded' or 'Build Failed' with the System Sounds menu in Windows |
| 105 | + |
| 106 | +## Extensions |
| 107 | + |
| 108 | +- [GitHub Copilot](https://github.com/features/copilot) - get suggestions to help write code based on your comments and other code open in Visual Studio (paid service with a free trial available) |
| 109 | +- [Spell Checker](https://marketplace.visualstudio.com/items?itemName=EWoodruff.VisualStudioSpellCheckerVS2017andLater) |
| 110 | +- [Live Share](https://visualstudio.microsoft.com/services/live-share/) to allow others to collaborate on the code you're working on |
0 commit comments