Tag Archives: Visual Studio 2015

VS2015 Preview: Layout Management

In earlier versions of Visual Studio, if you happened to mess up your window layout in Visual Studio, you could reset it back to the default layout by using the appropriate item in the Window menu:

vs2015-windowlayouts-duringreset

This rearranges the docked windows to whatever you originally had when Visual Studio was installed:

vs2015-windowlayouts-afterreset

That’s nice and all. But you may have noticed some new items above “Reset Window Layout” in the Window menu which are pretty handy when it comes to managing your window layouts.

For instance, when developing a new WPF application, the toolbox can sometimes come in handy for those without much experience with XAML. So, after introducing the Toolbox window, you can save the current layout:

vs2015-windowlayouts-duringsave

To save a layout, you need to give it a name:

vs2015-windowlayouts-promptsave

…and if that name happens to already exist, you’re asked whether you want to replace it (this is how you update saved layouts):

vs2015-windowlayouts-promptsaveexisting

You can then load (apply) these layouts by selecting them from the list in the Window menu, or by using the appropriate shortcut key (available for the first nine layouts in order). For instance, I saved this alternate layout suitable for unit tests, and I can apply it like this:

vs2015-windowlayouts-duringapply

…and then, like magic, the selected layout is applied:

vs2015-windowlayouts-afterapply

There’s also a menu item for management of these layouts:

vs2015-windowlayouts-managemenuitem

This opens a dialog which allows you to rename, delete, or reorder layouts.

vs2015-windowlayouts-managedialog

Reordering layouts has the effect of reassigning their keyboard shortcuts, since they are assigned in order to the first nine (from Ctrl+Alt+1 to Ctrl+Alt+9).

So, there you have it! Window layout management is yet another new feature in Visual Studio 2015 to improve your productivity.

VS2015 Preview: Live Static Code Analysis

In Visual Studio 2015, the new C# and VB .NET compilers are based on the .NET Compiler Platform known as Roslyn. This allows third-party tools to use the compiler itself to query the structure of code, rather than having to redo the compiler’s job. It also provides the ability to fix the code by modifying its structure.

You can use the NuGet Package Manager in VS2015 to install analyzers. At the moment there are very few available, and they’re still prerelease software. For this demonstration we’ll use the CodeCracker C# analyzer:

vs2015-analyzers-nuget

Once you install the package, you’ll see that the analyzer has been added under a special Analyzers node under the References in Solution Explorer. If you expand the analyzer, you can see all the rules that it enforces:

vs2015-analyzers-solution-explorer

As you can see, the code analysis rules may be enforced at various levels. Those marked as errors will result in actual compiler errors, and obviously cause the build to fail.

The rules that are broken by the code will then show up in the error list, which in VS2015 has been enhanced so that you can see more info about the error/rule and also click on the error code link to go to its online documentation (at the time of writing this article, the CodeCracker’s error code links are broken):

vs2015-analyzers-error-list

The best thing about live static code analysis is that they’re live: the rules will be checked as you’re writing your code, and you don’t even need to build for them to be evaulated.

Warnings may be sorted out by moving the caret within the relevant code and pressing Ctrl+. (Control Dot), after which the Quick Actions become available. See, code analyzers don’t need to limit themselves to complaining. If a fix is available, you’ll have the option to apply it, and you can preview the changes as with any other refactoring:

vs2015-analyzers-fix

If you know better than the code analyzer, you can opt to suppress the warning instead:

vs2015-analyzers-suppress

Code analysis allows rules to be enforced on your team’s code. Roslyn facilitates this by making it easy to plug in different code analyzers (and hopefully in future there will be a wider range to choose from) and by running code analysis live, without the need to build the project.

Note: this demonstration used the CodeCracker C# analyzer in order to show the different levels of rules (e.g. warnings, errors, etc), since the StyleCop analyzer’s rules are all warnings. The CodeCracker C# analyzer is in quite a mess at the time of writing, with missing fixes, broken rule documentation links, and countless grammatical errors. But it’s prerelease, so we’ll pretend that’s a good excuse and forgive it for its sins.

VS2015 Preview: NuGet 3 Preview

Well, what do you know. It seems like NuGet’s been to the hairdresser recently.  In fact, in Visual Studio 2015 you’ll find the preview of NuGet 3.0, which looks like this:

vs2015-nuget3-layout

It’s obviously changed quite a bit from the version we use today, which for the sake of comparison is this:

vs2015-old-nuget

The most obvious thing to notice is that NuGet is now a first-class citizen with its own page in Visual Studio, rather than being a little modal window. There are other layout improvements you’ll notice, such as the fact that they’ve done away with the separation between installed, installable and updatable packages. In fact, the new NuGet experience is a unified one in which you can filter the packages you want to see: All, Installed, or Update Available.

Usability is not all that is being improved in NuGet 3.0. In fact, there are a bunch of new features that weren’t in NuGet before. One that I am very happy to see is the ability to select the package version to install, rather than having to resort to the Package Manager Console to install specific versions.

Another really cool feature is that of package consolidation. Have you ever had something like 4 different versions of JSON.NET across your solution, and then had to painfully manually converge them into a single version? NuGet 3 allows you to consolidate different versions of the same assembly pretty easily. Just select the version you want, and select the “Consolidate” action, and click the “Consolidate” button:

vs2015-nuget3-consolidate

There are also a few other features, such as a Preview button showing what actions will be taken when you execute a change, and a couple of advanced options. For full details on what’s new, check out the official announcement.

Oh, and just in case you’re curious… that funny thingy next to the Search box that looks like a ship’s wheel is actually supposed to be a gear icon, because it takes you into the NuGet settings.

VS2015 Preview: Inline Temporary Variable

Visual Studio 2015 brings two new refactorings: Introduce Local Variable, and Inline Temporary Variable.

Inline Temporary Variable is actually the exact opposite of Introduce Local Variable. At times you’ll be using a variable for something so simple that it actually clutters the code. For example:

var firstArgument = args[0];
Console.WriteLine(firstArgument);

We can get rid of the firstArgument variable by using the Inline Temporary Variable refactoring.

To do this, we first need to select the firstArgument variable on the first line (where it is being declared), and then press Ctrl+. (Control Dot) or select “Quick Actions…” from the context menu after right-clicking the selection. This brings up the refactorings menu, from which we can select the refactoring we want:

vs2015-inline-temporary-variable

After selecting the Inline Temporary Variable refactoring from the menu, the code gets cleaned up pretty nicely:

Console.WriteLine(args[0]);

 

VS2015 Preview: Introduce Local Variable

There are two new refactorings introduced in Visual Studio 2015. The first of these allows you to introduce a local variable to simplify your code. Let’s see this at work on some source code from ImapTalk.

After selecting some code, press Ctrl+. (Control Dot) or select “Quick Actions…” after right-clicking on the selected code. You’ll get a list of refactorings that you can apply to your code, along with a nifty preview of what your code will look like after it is applied:

vs2015-introduce-variable-preview

In this code I’ve got this Color object that I’m passing directly into the constructor of a SolidColorBrush, which looks a bit messy. With this refactoring, I’ll introduce a local variable to hold that Color, and then pass the new variable into the SolidColorBrush:

vs2015-introduce-variable-renameOnce you select this refactoring, Visual Studio goes into inline rename mode, so that you can choose the name of the new variable.

Take a look at the refactored code above. Much better! 🙂

VS2015 Preview: Inline Rename

Another IDE experience that has been revamped in Visual Studio 2015 Preview is that of renaming stuff. You can rename a variable by right-clicking it and selecting “Rename…” from the context menu, or instead by simply hitting F2 on your keyboard:

vs2015-inline-rename-menu

When you do this, Visual Studio goes into Inline Rename mode. It finds all the instances of that variable and highlights them for you, while providing some other options on the side:

vs2015-inline-rename-renaming

As you type a new name, all instances are instantly updated:

vs2015-inline-rename-renamed

What’s really cool about this is that the IDE actually warns you if the new name conflicts with some other variable:

vs2015-inline-rename-conflict

Just press Enter to accept the changes.

Although I have demonstrated how to rename a variable, this also works for other things such as methods and classes. In case of conflicts, Inline Rename will attempt to resolve them using fully-qualified names if possible, as demonstrated in the video with Beth Massi and Dustin Campbell.

VS2015 Preview: Potential Fixes and Preview

When there’s a squiggly line in your code due to some error or warning, Visual Studio 2015 Preview provides an improved experience in dealing with them. When you hover over the squiggly line, aside from the fancy coloured tooltip, you’ll also get a link saying “Show potential fixes”:

vs2015-potential-fixes

Once you click on that “Show potential fixes”, you are given a list of actions. Note that you can also get to this list by clicking on the light bulb next to the tooltip, or else pressing Ctrl+. (read: Control Dot) on the keyboard while the caret is on the squiggly line.

vs2015-fixes-preview

When an item in the list is selected (but before you actually accept it), you get a handy preview (similar to what we saw with the unused usings) showing how the code will change as a result of that action. This means you will know what effect any fixes and refactoring (which will be covered in later articles) will have on your code before you take the plunge.

 

VS2015 Preview: Debugger support for lambdas and LINQ

If you’ve ever tried running a lambda expression in the Immediate Window or as a Watch, you’ll be pretty familiar with the response, “Expression cannot contain lambda expressions” (shown below in VS2012). If you try LINQ, you instead get “Expression cannot contain query expressions”, which is no better.

vs2012-lambda-immediate-window

This changes in Visual Studio 2015, which adds debugger support for lambda expressions and LINQ. VS2015 happily runs lambdas in both the Immediate Window and as Watches:

vs2015-lambda-immediate-window

LINQ works just as happily:

vs2015-linq-immediate-window

So there you go: another much-anticipated and extremely useful feature coming in Visual Studio 2015.

VS2015 Preview: XAML Peek

In Visual Studio 2015 Preview, you can now peek and edit definitions from XAML. Let’s take a look at some examples from Lilly Notes, a WPF application I built upon WPF MDI.

You can Peek Definition in your XAML editor via a context menu when right-clicking, or by pressing Alt+F12:

vs2015-xaml-peek-context-menu

If you do that on an event handler, you can peek into the event handler’s definition in the codebehind, without ever leaving your XAML:

vs2015-xaml-peek-edit

For more extended editing, you can “Promote to Document” (see screenshot above) to open the actual code file.

If you have an ElementName binding, you can peek into the definition of the target element, as you can see below with ToggleEditTitleButton:

vs2015-xaml-peek-elementname

XAML Peek also works pretty nicely with resources, which may be scattered about your project. For instance, I’m using this BoolToHiddenConverter as a StaticResource. I can peek its definition in the UserControl’s Resources section:

vs2015-xaml-peek-resource2

As it turns out, I can also peek the resource’s type (in this case BoolToVisibilityConverter), to edit the converter directly:

vs2015-xaml-peek-resource

No doubt this feature will be pretty handy for those writing some of the larger WPF or Windows Phone 8 applications.

VS2015 Preview: Unused usings/imports

Another of the IDE enhancements you’ll find in Visual Studio 2015 Preview is that unused using directives (C#) or imports (VB .NET) will appear in a less prominent colour, to distinguish them from the usings/imports that are actually used.

vs2015-unused-usings

You can see an example of this when creating a new WPF application (screenshot above), since most of the usings provided by default in the codebehind are useless until you require specific WPF features.

vs2015-remove-unused-usings-preview

That’s not all, however. On the side, you’ll find a light bulb that will suggest remedial actions. In this case, it’s suggesting that you remove the unused usings. When you hover over this action, you actually get a preview of what your code will look like.

You’ll see more of this paradigm of suggested actions and live previews as I continue to cover the new features in Visual Studio 2015 Preview.