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;
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:
After selecting the Inline Temporary Variable refactoring from the menu, the code gets cleaned up pretty nicely:
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:
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:
Once 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! 🙂
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:
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:
As you type a new name, all instances are instantly updated:
What’s really cool about this is that the IDE actually warns you if the new name conflicts with some other variable:
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.
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”:
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.
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.
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.
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:
LINQ works just as happily:
So there you go: another much-anticipated and extremely useful feature coming in Visual Studio 2015.
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:
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:
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:
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:
As it turns out, I can also peek the resource’s type (in this case BoolToVisibilityConverter), to edit the converter directly:
No doubt this feature will be pretty handy for those writing some of the larger WPF or Windows Phone 8 applications.
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.
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.
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.
Visual Studio 2015 brings a wealth of IDE enhancements. One of these is an improvement to the user experience of tooltips thanks to the addition of a touch of colour.
For instance, here’s what you get when you hover over a type:
…and this is the tooltip you get from intellisense:
How about this tooltip showing a preview of a collapsed method:
Or this tooltip showing a preview of XML documentation:
So that’s a nice touch of colour in several different tooltips.
Not impressed? Let’s take a look at what these looked like in Visual Studio 2013:
OK, so you’ll realise that this new feature in Visual Studio 2015 won’t change your life, but it’s a great improvement in user experience from what we had before.
I have released the second beta of IMAPTalk 2, featuring a number of minor fixes and enhancements. Visit the IMAPTalk project page to view the release notes and download the latest version.
I am currently installing the Visual Studio 2015 Preview on the machine where I have the Windows 10 Preview installed, and since it’s an old machine with just 1GB RAM, Windows started hanging at times. The screenshot below shows what the installer looked like while it was hanging:
See that tiny box towards the top of the screenshot? I guess that’s what windows actually look like before the clunky window style we’re familiar with gets rendered over it.
It’s nothing new – I’ve written before about legacy window styles appearing in Windows XP and this seems to continue that trend. The window style above has a very Windows 2000 feel to it, and I personally prefer it over what we have today in Windows 8+.