加载中

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

1,异步编程
因为在移动设备上,程序的响应速度实在是一个太重要的问题,所以对于应用被触摸后的反应也是同等重要。
在 Windows Runtime 中,设计师总是要考虑如果一个交互式操作需要耗费大于50毫秒的话,那么就该用异步编程来解决过慢的反应了,虽然同步编程很简单易懂,但是随着移动应用开发越来越火,如何更加方便的异步编程就是一个重要的话题了。

所以在制作 Metro App 时,C#独有的 async 与 await 关键字就是 C# 相比较 C++以及 JavaScript 在异步编程时的绚丽闪光点了;用 C++ 与 JavaScript,执行异步编程就意味着回调方法,甚至修改整个应用内核。然而 C# 的提供的异步编程方法能让你用同步编程的习惯写代码,却以异步的方式来实现,一举两得。

当然,说太多不能满足你的好奇心,去看看 Anders demo 演示的异步编程演示吧,你未来的Windows编程肯定少不了它了。

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!”

2,Windows Runtime 集成
Anders展示了新的Windows Runtime已经深度集成了.NET与C#,比如用C#作为"Compute engine"然后输出HTML/JavaScript的Metro App(简直就和C++一样)。所以你可以创建一个 C# 的项目,编译成 WinMD 文件,然后在你的 HTML/JavaScript 项目添加为引用。这样就能在JavaScript中智能地获取C#对象。正如Anders说到,"对于JavaScript程序员,你可能很幸运,但是对于C#程序员,你永远都是幸运的"。

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.

3,Caller Info 特性
Aforementioned Caller info 特性也被公布出来,这不算是一个惊人大发明,只是演示项目的代码调色清理盘而已。

因为对于C++程序员来说理解宏是很简单的,但是C#程序员就不一样了,所以我们在想一个新办法让C#也支持宏。
我们都知道C# 4.0已经支持了可选参数,那么C# 5.0就添加了 CallerFilePath与 CallerLineNumber以及 CallerMemberName 特性。

比如你可以像下面那样写一段 Trace.WriteLine 方法:


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);
    }
}
然后,如果你打算调用 Trace.WriteLine("Something just happened"),那么编译器将会自动依据你在哪里调用这个方法的信息来填写完剩下的可选参数。
注意这个特性在开发者预览版里是没有的,将在最终发行版首次亮相,正如Anders说到,这算不上一个大大的惊喜,但是不用怀疑很多人都会喜欢用它。


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.


4,The Roslyn (Compiler APIs) Project
Anders介绍到Roslyn时说到因为我们越来越沉迷与IDE提供的便利,比如查找引用,搜索Symbol,重命名以及提取方法等等。但常识告诉我们有个前提就是代码是必须锁定在编译器内的。

Roslyn 就是一个内建API编译器,它的输出就如同当前项目的代码编译器一样,将包含语法树API,标记 API,绑定分析流API以及发行API等等。而且这些API本身就是依据现代API设计原理来的。

以上的所有特性在10月中旬就可以使用了,当 Visual Studio Roslyn 的CTP版本发布时,C#与VB的API就能正常使用。 
返回顶部
顶部