Friday, 16 September 2011

What’s new in C# 5.0 and VB.Net?

As always, Anders Hejlberg’s session at Microsoft’s Build conference was a must-see. He’s a great presenter, and he had some great things to show off.

Anders started off by laying out what’s new in C# 5.0 and VB.Net. Bear in mind that Microsoft now have a policy of language parity for C# and VB.Net. So both languages get Windows Runtime support, Asynchronous programming and a brand new feature, Caller info attributes. VB.Net plays catch-up, and finally gets support for Iterators.

Asynchronous Methods

The first demo in Anders talk was about asynchronous programming. He introduced it by reminding us how vital responsiveness is in touch applications. This was something Jensen Harris mentioned in his talk. Touch User Interfaces are visceral. If an application doesn’t respond immediately when you touch it, it feels dead, whereas using a mouse or keyboard introduces a kind of disconnect between you and the computer which makes delays feel more acceptable.

In the Windows Runtime, the designers took the decision that if an operation was going to take more than 50 milliseconds to complete, it must be implemented asynchronously. There will be no synchronous equivalent – it would make it too easy for developers to do the lazy thing. So, all of a sudden it becomes really important to be able to do asynchronous programming easily.

This is where C# shines in comparison to C++ or Javascript for writing Metro style apps. In those languages, doing asynchronous programming means callbacks, and that means turning your programming inside out. The Asynchronous Methods feature in C# allows you to do async programming in the usual, straightforward, do-this-then-that fashion.

I won’t say any more on that here. Go watch Anders demo if you haven’t groked async yet. Your future as a Windows developer depends on it!

Windows Runtime integration

In the next section of his talk (starting at 26:00), Anders demonstrated more of the deep integration that C# and .Net now has with the Windows Runtime. He showed how it is possible to take a C# “compute engine” and expose it to a HTML/Javascript meto-style app (it works just as well with C++). You can take a C# project, compile to a WinMD file, then reference it from your HTML/Javascript project. This gives you intellisense for your C# objects when you access them from your Javascript code. As Anders said, “In Javascript, you sometimes get lucky. [Beat]. Of course, in C# you get lucky always! … I’m Just saying!”

Caller Info Attributes

The aforementioned Caller info attributes came up next (see video at 32:00). Not a big feature, just a palate cleanser between demos, according to Anders.

Apparently the C# team often get asked when they’re going to introduce support for __FILE__ and __LINE__ macros, which, C++ developers will know, expand when compiled to produce a string containing the name of the current source file, and the current line, respectively. Of course, C# will only introduce support for macros over Ander’s dead body, so the team had to find another way.

Anders related that back in the days of C# 1.0 they had a design that implemented this feature using attributes, but it involved optional parameters on methods which weren’t supported at the time. Well, now C# 4.0 supports optional parameters on methods, so the team revisited the design, and, lo and behold, C# 5.0 now has the CallerFilePath, CallerLineNumber and CallerMemberName attributes.

You can write a Trace.WriteLine method like this:

public static class Trace
{
    public static void WriteLine(string message,
        [CallerFilePath] string file = "",
        [CallerLineNumber] in line = 0,
        [CallerMemberName] string member = "")
    {
        var s = string.Format("{0}:{1} - {2}: {3}", file, line, member, message);
        Console.WriteLine(s);
    }
}

Then, if you were to call Trace.WriteLine(“Something just happened”) the compiler would automatically fill out the optional parameters in the WriteLine method with details of where you made the call from.

This isn’t in the current developer preview, but will be in the final release. As Anders said, not a big feature, but no doubt somebody will put it to good use.

The Roslyn (Compiler APIs) Project

The last half of the talk (starting at 35:26) dealt with what comes after C# 5.0, which is what project “Roslyn” in all about (named apparently after a cute little mining town, an hours drive from Seattle). Roslyn is “the compiler re-imagined”. Anders introduced Roslyn by noting that we are increasingly addicted to IDE productivity tools which provide features like “Find Reference”, “Go to Symbol”, “Rename”,  and “Extract Method”. All these need knowledge about code that is traditionally locked inside the compiler.

Roslyn is all about building compiler APIs that expose what the compiler knows about code to the IDE and to developers. It will include Syntax Tree APIs, Symbol APIs, Binding and Flow Analysis APIs and Emit APIs. The APIs are designed using modern principles. So, for example, the SyntaxTree objects are completely immutable (though you can create new ones by combing bits from old ones) and this means that they are concurrency safe. All the language services in the IDE (syntax colouring, intellisense, etc.) are going to be using Roslyn.

All this is going to be available to us starting mid October, when a CTP of Visual Studio Roslyn will be released. APIs will be available for both C# and VB.

The demos Anders gave of Roslyn are pretty impressive (starting 43:00). First up was the ScriptEngine API that allows you to parse and execute code from a string, or even get a delegate back for executing it at some later point. This led in to the demo of the C# interactive window, which will be part of Visual Studio in the future. You can select code in the editor, press Alt-Enter and have it immediately executed in the interactive window. If you then go and edit it there, you get intellisense, even for methods that you have just interactively defined.

Another demo,roundly applauded, was “Paste as VB” and “Paste as C#” which uses the Roslyn APIs to parse and translate code from the Clipboard buffer. The source code for this will be made available in the CTP.

None of this was big news, of course, because Anders first started talking about it when I was at PDC in 2008. But it’s nice to know that it’s moving on steadily, and that we’ll soon be able to have a play with it.

Windows Runtime – Dial in the right expectations before reading up

Three days on from the announcement of the Windows Runtime at Microsoft’s Build conference the marketing dust is settling and the full picture emerging. As you begin to read up what the Windows Runtime actually is you’ll need to set your expectations to the right level or you run the risk of being disappointed. But set them appropriately and I think you’ll see that an exciting developer journey lies ahead.

Leading up to the conference I was hoping for a new lean-and-mean User Interface platform for writing any kind of Windows application. But that isn’t what Windows Runtime is about. As things stand this week, Windows Runtime is about producing Metro-style applications (think Windows Phone 7) optimised for running on Tablet devices. Windows Runtime is aimed squarely at the solar plexus of iOS and Android 3.0.

Miguel de Icaza and Tim Anderson have already done a good job of writing up what has been announced so far about Windows Runtime (or WinRT). But here are the two key points to notice:

  • Metro-style apps run in a sandboxed environment. As with Windows Phone 7, apps have to declare upfront, through a manifest built in at compilation time, which capabilities of your PC they intend to use. Access to APIs is monitored at runtime by the Runtime Broker, which prevents your app from doing anything naughty. Only certain parts of the .Net Base Class Libraries can be used, and even native developers are limited as to which Win32 or COM objects they call. Apps can only talk to each other through the WinRT-endorsed Contracts, which allow for Sharing, File-picking etc.

    To a developer all this sounds like a horrible restriction. But it is wonderful for users, who can have much more confidence that applications they install from the new App Store will not get up to mischief.

  • Metro-style apps are always full screen and immersive. Windows Runtime does not provide a general-purpose windowing system. Several presenters at the conference mentioned that support for overlapping windows have been intentionally left out. And for one simple reason: they don’t work well in a touch-centric environment.

Windows Runtime is not (at the moment) a general purpose runtime. It may get there eventually, but for the time being it is focused on building the kinds of apps and games that today you might run on the iPad, the Xoom, or (once the lawyers have finished their fisticuffs) the Galaxy Tab. Think of it as Silverlight-on-Steroids-With-Native-Extensions for Tablets and you won’t go far wrong.

As speakers at Build conceded, there are many kinds of application that would not fit the Metro-style. Visual Studio is one. Photoshop is another. Microsoft Office is (possibly) a third. For these kinds of apps, keyboard, mouse, menus, toolbars, ribbons and dialog boxes still works best.

This means Win32 and WPF are still very much alive. Though there are no sessions about it at the conference, WPF 4.5 has been announced with a number of not-insignificant new features, including the promised fix for the Win32 airspace restrictions (though nothing has been said about hosting Silverlight controls in WPF apps).

I’m no prophet, but I don’t believe this is the last we’ll hear about WPF.

Wednesday, 14 September 2011

Build Windows Day 1–and a peek a what’s to come

If you haven’t caught the news already, yesterday at Microsoft’s Build Windows conference Steven Sinofsky announced a preview of Windows 8 and a bunch of developer tools, including Visual Studio 11. You can download it here.

As predicted, Windows 8 comes with an all new development framework, the Windows Runtime (WinRT). I’ve not read enough myself to begin explaining. But here’s a picture, snapped during Steven Sinofsky’s keynote:

Windows 8 Platform and Tools - How WinRT relates to Win32 and .Net

WinRT is an object-oriented API, consisting of 1800 objects, accessible to managed, unmanaged and JavaScript code alike. Sinofsky emphasised that this WinRT is a peer to Win32, not another abstraction layer built on top of it. As this diagram clearly shows, and Sinofsky stressed in his talk, Win32, .Net and Silverlight developers have nothing to fear, all current frameworks continue to work in Windows 8.

I’m sure to have more to say about WinRT in the coming days. In the meantime, InfoQ has a couple of articles summarising what we learned in the keynote. If you’re wanting a sneak peak at the API’s themselves, check out the preview documentation on MSDN.

The videos are the first few sessions of the conference are already up on the web:

What’s next?

In other news, the session list is now up for the rest of the week. Here are a few that grab my attention.

And now, on with the show!

Wednesday, 7 September 2011

Microsoft’s Build Windows Conference is Next Week – What’s in Store?

It’s less than a week to go until Microsoft’s Build conference. According to tradition, about now I should be speculating on what the future holds for us faithful Microsoft developers. But there’s still no official session list. Fortunately, we do have a few official hints, and a couple of very unofficial leaks to whet our appetites for what’s in store.

Windows 8

Read the Build homepage, and you’ll see that the conference is focused very clearly on Windows 8. We’ve already had a preview of the new touch-centric, Windows Phone 7-like UI. And over on the Building 8 blog, Steven Sinofsky and others on the Windows team have started announcing some of the to-be-expected features like USB 3.0 support.

Here are some of the other things that we know already:

And now comes the controversial part. Those of you who have not entirely delegated their memories to Google will recall that when Microsoft first showed the new Metro desktop for Windows 8, they announced that developers would be able to use HTML5 and JavaScript for building these new-fangled immersive apps. But they said nothing about the presence of .Net, WPF or Silverlight in this brave new world, stirring up an instant furore in the blogosphere.

But there’s no need to panic. It’s all under control. I think.

Direct UI

This is where we turn to the leaks. Leaked Windows 8 builds, that is.

According to Peter Bright over on Ars Technica, Microsoft are creating a new Windows Runtime (or WinRT), which is intended to be the successor to the Win32 API. It will be a native-code API, but shaped in a way that is pleasing to the eye of a managed-code developer, and what is more, said .Net developers will be able to access WinRT through a new (hopefully painless) version of COM for which support is being built into .Net 4.5.

Included in WinRT is a new UI framework called Direct UI which appears to be a lean-and-mean version of WPF/Silverlight (and also uses XAML – remember how part of the XAML team got moved to the Windows division!). With the official details coming out in less than a week, there’s little point on me elaborating further here, but you can get a taster of the new APIs by reading these two forum threads which dissect some of the leaked Windows 8 builds.

What I look forward to hearing is where WPF fits into the picture. One thing we know with a high degree of confidence, given Microsoft’s backwards-compatibility track record: current WPF applications will continue to work. The question is: will there be further development to WPF? As I reported last year, we know there are some new features coming, including fixing the airspace issues when hosting Hwnds inside WPF controls, and enabling hosting of Silverlight controls. But will there be anything beyond that? And will there be interop between WPF and Direct UI? Watch this space.

Silverlight

Remember that Microsoft stirred up another firestorm at PDC 2010 by staying mum about Silverlight? They put that right a few weeks later by announcing Silverlight 5, which was quite distinctly not a maintenance release, since it included a whole raft of new features like a 3d API, vector printing support, and P/Invoke for Trusted Applications. They’ve now made good on that, with the Silverlight 5 Release Candidate coming out just last week. It will be interesting to learn Microsoft’s vision for how Silverlight, WPF and Direct UI align.

C# 5.0

We already know the headline feature for C# 5: asynchronous methods. We’ve had a CTP. Here’s hoping for a beta release during the conference. One thing Anders did say at his talk last year is that async won’t be the only new feature in C# 5.0. So I wonder what else he has up his sleeves? It would be nice if it was the Roslyn project (Compiler as a Service).

Visual Studio

It sounds like Microsoft are preparing to release a new preview build of Visual Studio at Build. Many of the features that were previously released as PowerToys for VS 2010 are going to part of vNext. But the more interesting news to me is that Microsoft have been taking note of data coming back from PerfWatson to make some big performance improvements. Visual Studio vNext is going to make more use of multi-core machines, and will reduce memory usage in some cases by doing builds out-of-process for C# projects.

Stay Tuned

My new boss has kindly given me some time next week to follow the Build conference from the comfort of my office. And as in previous years, I’ll be reporting back the choicest titbits as I find them. Follow me on twitter to hear it as it happens.

Now, over to you. What are you looking forward to? Have you heard any rumours that I’ve not picked up on?