During this week's Build conference, Microsoft unveils the new Windows and many of its features, using the new WinRT. During Aleš Holeček's talk, selecting a file from C# using the new library was shown. The interesting part is that it was asynchronous and used the new C# 5
So, I started preview Visual Studio 11 from preview Windows 8 running as a virtual machine in VirtualBox (which took some figuring out to set up properly) and went to try it by myself. The code looks like this:
var picker = new OpenFilePicker(); var file = await picker.PickSingleFileAsync();
If you read something about
await, you know that what it actually does is to call the
GetAwaiter() method (which can be an extension method) on the result of the expression it's used on. In this case, it's the result of
PickSingleFileAsync(), which is a
PickSingleFileOperation. This class implements the
IAsyncOperation interface, but doesn't seem to have any
GetAwaiter(). So, it has to be an extension method, right? Except extension methods should show up in IntelliSense, which this one doesn't.
To solve this mystery, I looked at the generated code in Reflector. Now, inside the state machine, there is a call to
WindowsRuntimeSystemExtensions.GetAwaiter(). So it is an extension method after all? How come I didn't see it? Turns out, that method has an
[EditorBrowsable(EditorBrowsable.Never)] attribute on it, which means it doesn't show up in Visual Studio, but can still be called.
Thus ends the mystery of the missing
GetAwaiter(). C# 5's
await wasn't revamped because of Windows 8, as I initially suspected. It's just that WinRT hides some complexities that should be hidden to ordinary developers anyway.