Visual Studio Tips 4

OK, so this is another round of tips on using Visual Studio! See first collection here, second here and third here.

  1. Create shortcuts: you can create shortcuts to frequently used code. Just click <Ctrl>KH on any line, and you will add a shortcut to it:

    image
    After that, you can jump to any of your bookmarks from the Task view:
    image
    To remove it, click <Ctrl>KH again in the same line. Note that this is not the same as the bookmarks featured introduced in the third set of tips!
  2. Code Maps: this requires Visual Studio Enterprise. Open the Architecture menu and select Generate Code Map for Solution.

    image
    You will get a nice diagram showing all dependencies between assemblies in your project:
    image
  3. Code Metrics: get a static analysis report of your project, only in Enterprise edition. In the Solution Explorer, right click on your project, Analyze, and select Calculate Code Metrics.

    You get an analysis including metrics Maintainability Index, Cyclomatic Complexity, Depth of Inheritance, Class Coupling and Lines of Code, which you can even export to Excel:
    image
  4. Format document on save & remove unused and sort using statements on save: Visual Studio can take care of formatting your documents and removing any unnecessary using declarations for you, when you save your documents; you just need to install the Productivity Power Tools extension and enable these options in ToolsProductivity Power ToolsPowerCommands:
    image
  5. Thread Deadlocks: Visual Studio can show you thread deadlocks while debugging. When debugging a multithreaded app, break it and open DebugWindowsTasks:
    image
    From here, you can flag your code so that it is more clearly visible.
  6. Executing code in Immediate Window: the Immediate Window can be used to run .NET code. While debugging your app, pause it, and select DebugWindowsImmediate:

    image
    You can enter any .NET expressions and variables or even complex expressions and see them evaluated at once.
  7. Pin and export Data Tips: while debugging, break and hover any variable or field in your code. Click the pin icon on the right of the tooltip window. This will create a Data Tip.
    image
    You can have multiple Data Tips, add comments to them:
    image
    Also save Data Tips to files so that they can be loaded in another Visual Studio session, and, of course, load a collection of Data Tips:
    image
  8. Place reusable code in Toolbox: you can add code that you use frequently to the Toolbox. Pin your Toolbox window, highlight your code and drag it to the Toolbox. Even if you are not working in a visual app, the code snippet can be dragged from the Toolbox into your code:
    image
  9. Change C# language version: Visual Studio can use any of the C# versions. For example you may or may not want to use the latest (and greatest, by the way!) features of C# 6. All you have to do is right click on a project, Properties, Build and Advanced. In the Advanced Build Settings window, Language Version, select the one you want to support:
    image
  10. Find tips: some tips related to the Find functionality. First, you have two results windows, 1 and 2, and you can have your search results appear in any of them:
    image
    With this, you can also append to current results or show only the file names that match the search pattern.

Visual Studio Tips 3

Next round of Visual Studio tips! 10 more for you! Here you can find the first and the second posts.

  1. Change solution version: you can change the Visual Studio solution version by opening the .SLN file and changing its header:


    Microsoft Visual Studio Solution File, Format Version 12.00
    VisualStudioVersion = 12.0.30110.0

    =Visual Studio 2013

    Microsoft Visual Studio Solution File, Format Version 12.00
    VisualStudioVersion = 14.0.23107.0

    =Visual Studio 2015

  2. Even if you didn’t declare a variable for the exception in a catch block, you can still see it by adding the special keyword $exception to the watch window:

    image
  3. Scroll bar options: the vertical scroll bar can show hints for a number of different things, or even display a map of the lines in its window. You can choose what it should be used for by right clicking on the scroll bar and clicking on either bar mode or map mode and selecting changes, marks, errors, etc:

    image
  4. Show line numbers: besides having version numbers on the status bar, you can also have them on the left; go to ToolsOptionsText Editor All Languages and select Line Numbers:
    image
  5. Move lines up or down: you can move an entire line up or down; just click on it and press <Alt>-<Up> or <Alt>-<Down>; you can also duplicate it with <Ctrl><C> + <Ctrl><V>, no need to actually select it all;
  6. Add search box to the toolbar: on the standard toolbar, click the button with the triangle pointing down, then select Add or Remove Buttons:

    imageand then choose Find; you will get a search box for your source code:
    image
  7. Open multiple browsers at once: you can open your page in different browsers at the same time, automatically, upon starting (<F5>) or debugging (<Ctrl><F5>); just select the down arrow next to the browser selection
    Browse WithThen select Browse With:
    Default Selected Browser

    And select several at once:
    Multiple Default Browser
  8. Change CodeLens options: right clicking on CodeLens will bring the options dialog:
    imagefrom which you can change what to display:
    image
  9. Change variable value from the debugger: if you have a variable, settable property, field or parameter in the watch window, you can change its value by clicking on the right column and entering an appropriate expression:

    image
  10. Add bookmarks: you can easily add bookmarks to lines of code by clicking <Ctrl>+<K>; to remove a bookmark, click <Ctrl>+<K> on the bookmarked line of code; <Ctrl>+<K> followed by <Ctrl>+<N> will navigate to the next bookmark and <Ctrl>+<K> followed by <Ctrl>+<P> to the previous one; adding the <Shift> key to <P> or <N> will do the same, but inside a solution folder.

And that’s all for now. Stay tuned for more!