探索!全新一代 Visual Studio 11[持续更新]
今天微软发布了Visual Studio 2011 Developer Preview,由于糟糕的线路,暂时还在下载中。
计划下载之后第一时间向大家汇报 VS 11 的体验感受。
已经下载完成,稍后安装。不过大家注意,只有Windows 7、Windows Server 2008 R2和Windows (Server) Developer Preview支持VS11,如果你还在使用Windows XP、Windows Server 2003和Windows Vista,那么很遗憾,在你打算升级操作系统之前就不用下载VS 11了。
先给出一些新特性,这些足够激动人心!
先看Visual Basic
Visual Basic 缺少一个 C# 2005 开始增加的特性,就是迭代器。
在C#中,我们可以使用yield return来实现迭代器,比如:
- C# code
IEnumerable<string> split(string, source, char c){ string s = ""; for (int i = 0; i < source.Length; i++) { if (source[i] == c) { yield return s; s = ""; } else { s += new string(new char[] { source[i] }); } } if (s != "") yield return s;}
调用:
- C# code
foreach (string s in split("hello world csharp", ' ')){ Console.WriteLine(s);}
在Visual Basic中,很难实现这样的功能,一种简单而且稳妥的办法是,使用List<string>代替IEnumerable<string>,这样我们就只能在所有项都找到以后才开始迭代了。
之所以迭代器难以实现,是因为C#编译器在内部为我们的迭代器创建了一个线程,并且使用了从循环外向循环内跳转的转移指令,这样就可以在迭代触发的时候保存当前堆栈,这很难用合法的VB语法描述。
现在,Visual Basic 11,终于支持yield,而且写法比C#更简单:
- VB.NET code
Sub Main() For Each number As Integer In SomeNumbers() Console.Write(number & " ") Next ' Output: 3 5 8 Console.ReadKey()End SubPrivate Iterator Function SomeNumbers() As System.Collections.IEnumerable ' Use multiple yield statements. Yield 3 Yield 5 Yield 8End Function
对于C#来说,最激动人心的增强是异步语法,异步语法有2大用途,一个是增强程序的并行计算能力,提高性能。另一个是在下一代应用程序(包括Windows 8的程序),异步从服务器端获取数据并且处理成为一种通用的、常见的代码架构风格。客户机和服务器的通讯延迟的不确定性使得那种传统的Web程序(延迟放在服务器端)和桌面程序(几乎没有延迟,或者有可控的延迟)以及架构成为落伍的东西。
而且高兴的是,异步程序编写在C# vNext上非常简单,微软的文档指出,将同步的代码转换为异步只需要简单的几步
为方法加上 async 修饰
将返回值从 byte[] 转换为 Task<byte[]>
在方法调用前加上 "Async
在方法体内作如下修改
将方法由同步的 Get 变成异步的 GetAsync
对结果使用 await 运算符
就这些!看代码:
- C# code
// Synchronous version of a method that downloads the resource that a URI// links to and accesses its content.private byte[] GetByteArray(Uri currentURI){ // Declare an HttpClient object and increase the buffer size. HttpClient client = new HttpClient() { MaxResponseContentBufferSize = 1000000 }; // The Get method returns an HttpResponseMessage that contains the // content of the resource along with other information. HttpResponseMessage httpRM = client.Get(currentURI); // Use ReadAsByteArray to access the content as a byte array. return httpRM.Content.ReadAsByteArray();}// Asynchronous version of the same method.private async Task<byte[]> GetByteArrayAsync(Uri currentURI){ // Declare an HttpClient object and increase the buffer size. HttpClient client = new HttpClient() { MaxResponseContentBufferSize = 1000000 }; // The GetAsync method returns a Task(Of T), where T is an HttpResponseMessage. HttpResponseMessage httpRM = await client.GetAsync(currentURI); // Use ReadAsByteArray to access the content as a byte array. return httpRM.Content.ReadAsByteArray();}
关于异步编程,我将在第一时间体验并且给出更详细的介绍。
对于F#,最大的改进是查询表达式。
看下面的代码
- C# code
[<Generate>]type Northwind = ODataService<"http://services.odata.org/Northwind/Northwind.svc">let db = new Northwind.NorthwindEntities()// A query expression.let query1 = query { for customer in db.Customers do select customer }query1|> Seq.iter (fun customer -> printfn "Company: %s Contact: %s" customer.CompanyName customer.ContactName)
我不知道是否应该叫F#版的LINQ,但是这和Ruby又很像。试想,F#有了查询表达式,它很可能被用来作一个轻量的数据库访问脚本编写工具语言。
对于 VS11 面向的 .NET Framework 4.5
我觉得最激动人心的是对 HTML 5 的全面支持
包括 HTML5 新增的表单,全面支持低调的JS调用,客户端验证(我一直不知道低调的JS这个怎么翻译比较好,它的意思是,将JS从HTML代码中剥离出来,也就是那种<button id="button1" onclick="javascript:xxxx" />这样的代码被转换为 $("button").onclick(xxx)。)
集成AntiXSS从而解决困扰Web开发的XSS(跨站)攻击
对WebSockets协议的支持,这个非常重要,从此Web将实现双向通讯。那些WebIM技术将很容易实现。
对HTTP requests 和 responses 异步访问的支持
在ScriptManager中支持CDN加速。
[解决办法]
线路确实很糟糕,我正在用win8的开发预览版,这个版本没有带vs11,本来想再下,但是实在太慢了……
vs11的新特性可以参考这个:
http://sd.csdn.net/a/20110915/304448.html
[解决办法]
What's new in Visual Studio 11
http://channel9.msdn.com/events/BUILD/BUILD2011/TOOL-820F
================
谢谢督察的补充。
[解决办法]
Microsoft® Visual Studio® 11 Developer Preview (ISO)
http://www.microsoft.com/download/en/details.aspx?id=27538
[解决办法]
[解决办法]
支持,很强大
[解决办法]
看看 先
你们速度太快了
没精力搞这个
你们研究吧 到时候共享下
[解决办法]
主要还是对 Metro UI Application 的支持。提供对 Windows 8 WinRT 的调用API。
装了个 Windows 8 Developer Preview,默认安装了VS11, 这个月都有事干了。
[解决办法]
[解决办法]
win8 DP 安装迅雷7以后IE就打不开了,有谁遇到这个情况了?
[解决办法]
这么快。。。