Visual Studio Tips 5

This is the fifth post of my Visual Studio tips series. See the first one here, the second here, the third here and the fourth here.

  1. Abort build: before exiting Visual Studio or retrieving changes from source control, any ongoing build must finish. If you don’t want to wait, you can abort it by pressing <Ctrl><Break>
  2. Start Visual Studio in safe mode helps in debugging problems introduced by some extensions. In the command line just enter: devenv /SafeMode
  3. Object IDs: you can track variables and fields in the debugger even when they go out of scope by creating an object id (1, 2, etc) for it. First add the variable or field to the watch window and then select Make Object ID:
    Then you can inspect it at any time you want just by adding it to the watch as $1, $2, etc:
  4. You can now add breakpoints to Auto-Properties, in the getter or setter! Cool!image
  5. NuGet Package Suggestion for Unrecognized Types:Visual Studio can suggest automatically installing a specific NuGet package to resolve an unknown type found in the code, or one that you may have typed yourself. This needs to be enabled.image
  6. Query Syntax in Go To All (Ctrl+T): when querying Go To All, you have some prefixes to help narrow your search results:
      • f {file}: for files
      • t {type}: for types

    • m {member}: for any type member
    • # {symbol}: for any symbol
  7. No side effects: sometimes, when debugging, we may need to evaluate an expression (property, method) that causes side effects, like, changes the internal state of some class. When we don’t want this to happen, we can add nse to the end of the expression, and it will prevent any unwanted side effects:image
  8. Copy files upon project build: you can use Visual Studio (or, better, MSBuild) to copy files for you during project build. Just add something like this to your .csproj:image
  9. Paste ring: Visual Studio keeps several items in its internal Clipboard. You can iterate through them all by clicking Ctrl+Shift+V:
  10. Using Visual Studio to compare files. There are two ways:
    1. Using the command line: devenv /diff “Program.cs” “Program – Copy.cs”
    2. Using the Command Window: Tools.DiffFiles “Program.cs” “Program – Copy.cs” (shown below)


And this is it for now! Stay tuned for more!

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:

    After that, you can jump to any of your bookmarks from the Task view:
    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.

    You will get a nice diagram showing all dependencies between assemblies in your project:
  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:
  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:
  5. Thread Deadlocks: Visual Studio can show you thread deadlocks while debugging. When debugging a multithreaded app, break it and open DebugWindowsTasks:
    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:

    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.
    You can have multiple Data Tips, add comments to them:
    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:
  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:
  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:
  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:
    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:

  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:

  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:
  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:
  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:
  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:

  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!