C# Interactive Initialisation Trick

Hi there,

A little aside this one; something very, very cool that I’ve noticed with the advent of Visual Studio Update 2. Without question, this rates as a MUST CHECK OUT Visual Studio feature.

The first complaint (and a valid one, no question about it) in regards to the C# Interactive Window was that running your own code was a little bit of a painstaking process, requiring you to load dlls and supporting references manually, using the #r command. Now, with update 2, initialising C# Interactive with your applications context is a cinch.

For starters, right-click on the project you are interested in and select the Initialize Interactive with Project option, within the Solution Explorer:

Showing how to Initialise Interactive with Project.

Initialise Interactive with Project Option.

This, as you’ll see, does all of the ‘scaffolding’ work for you within the C# Interactive Window, and injects a using statement for the default namespace to get you started. You should see something like the following:

C# Interactive Window initialised so that the Project is in scope.

C# Interactive Window Initialised with Project.

From here on in, you are free to start adding using statements, as required, and begin writing code against the types in the target project; in a completely pain-free fashion. A massive thumbs up…you can now go wild, like this for example:

Example of using Project Code from C# Interactive.

Using Project Code from C# Interactive.

I did try this on a larger, more complex and tightly-coupled code base and I can comfortably say you’ll have issues depending on how things are structured. Smaller, more loosely-coupled types, where SOLID principals have been adhered to are going to allow you to use C# Interactive in this way with more ease.

It is also possible to highlight portions of code, in any file, and right-click the Execute in Interactive option to run with, and execute, smaller chunks of functionality.

Posts on the way to cover some elements of the ASP.NET Web API and Ninject Dependency Injection (hopefully, at some point over the weekend), so watch this space.

Have a good weekend all and I’ll be back with you in a jiffy…

Visual Studio 2015 First Impressions – PerfTips

I’ve had a super quick tour of Visual Studio 2015 Community Edition and I have to say I’m pretty impressed as it stands. I’m currently downloading Unity 5 and will be installing the latest toolkits shortly that go hand in hand with this.

In the meantime, I’ve been looking at some of the new debugging performance aids; namely, PerfTips.

Full information can be found here:

Visual Studio PerfTips

In the example screenshot below I have rigged some methods (using the new async syntax in a rough and ready way) to simulate a slight delay in the retrieval of information. I’ve placed breakpoints in the code and you can see that, when hit, a small diagnostic message is shown in the editor detailing the time, in milliseconds, that have elapsed during the previous method call. Very, very nice:

PerfTips In Action

PerfTips In Action

You will also notice that some extra diagnostics, listing the events that have occurred so far and interesting metrics such as memory usage, are being shown in the Diagnostic Tools window on the far right. This is something I’ll have a further dig into; looks interesting on first glance.

The only thing I didn’t like off the bat was the default colour of the PerfTip text itself (which is non-descript when not highlighted, as standard). I have a penchant for the Visual Studio Dark theme so I prefer colours to pop a little more, just personal taste really. Thankfully, this can be easily rectified by navigating to Tools > Options > Environment > Fonts and ‘Colours’ (sorry, I’m British, I can’t help myself!) as shown here:

PerfTips Before Customisation.

PerfTips Before Customisation.

From here, you can adjust the Item foreground value (ensuring that PerfTips is selected from the Show Settings for drop down and that Text is highlighted in the Display items selection box). Of course, you’re also free to set the colour of the PerfTip text when highlighted as well by selecting the Highlighted Text item from the Display Items selection box and adjusting the Item foreground to the desired colour. If the colours on offer don’t float your boat then the usual custom colour picker is present also.

You can fly by this handy, dandy post for more details:

Customise PerfTips

The end result (depending on the colour you pick) is as follows:

PerfTips After Customisation.

PerfTips After Customisation.

I did receive a message stating that the application may need a restart for changes to take hold. In this case, I got away with it and the change kicked in immediately.

Anyway, just a short post for tonight. I will detail any other interesting finds as they crop up.

Until the next time!